【vue前端】关于ts项目中常见写法的一些总结

1、目录如下:

 

 2、源码如下

child.vue

复制代码
<template>
  <div>
    <div>{{ propVal }}</div>
    <el-button @click="emitVal('我是儿子')">我是子组件</el-button>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch, Emit, Model, Ref } from 'vue-property-decorator'

@Component({
  name: 'Child'
})
export default class Child extends Vue {
  @Prop() private propVal: any
  // Emit子组件触发
  @Emit('emitValFun')
  private emitVal(val: string) {
    return val
  }
  // Model
  @Model('click') readonly value!: string
}
</script>

<style lang="scss" scoped></style>
复制代码

index.vue

复制代码
<template>
  <div>
    <el-button>我是父组件</el-button>
    <child :propVal="propVal" @emitValFun="selfFun" value="我是value"></child>
    <div id="login_container"></div>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Watch, Emit, Model, Ref } from 'vue-property-decorator'
import child from './child.vue'
import Mixin from './mixins'

interface stringVAl {
  form: string
}
@Component({
  name: 'my',
  components: {
    child
  },
  mixins: [Mixin]
})
export default class Test extends Vue {
  private text: stringVAl = {
    form: '字符串'
  }
  private propVal: any = '父组件给子组件传值'
  // 监听
  @Watch('text', { deep: true })
  changeText(val: any) {
    console.log('watch监听')
  }
  // 计算属性监听
  get textVAl() {
    return this.text
  }
  // Emit父组件接收
  selfFun(q: string) {
    console.log(q, '父组件接收子组件值')
  }

  setWxerwma() {
    // const obj = new WxLogin({
    //   self_redirect: true,
    //   id: 'login_container', // 需要显示的容器id
    //   appid: 'wxef5765dd395d8504', // 公众号appid wx*******
    //   scope: 'snsapi_login', // 网页默认即可
    //   redirect_uri: encodeURIComponent('https://passport.zcool.com.cn/thirdlogin/wechat_callback.do?appId=1006'), // 授权成功后回调的
    //   state: Math.ceil(Math.random() * 1000), // 可设置为简单的随机数加session用来校验
    //   style: 'black', // 提供"black"、"white"可选。二维码的样式
    //   href: 'https://static.zcool.cn/passport4.0/css/wxCode.css?v=0.1' // 外部css文件url,需要https
    // })
  }
  mounted() {
    this.setWxerwma()
  }
}
</script>

<style lang="scss" scoped></style>
复制代码

minixs.vue

复制代码
/**
 * 这个组件完全依赖于vue-class-component.它具备以下几个属性:
@Component (完全继承于vue-class-component)
@Emit
@Inject
@Provice
@Prop
@Watch
@Model
Mixins (在vue-class-component中定义);

作者:Homary
链接:https://www.jianshu.com/p/d8ed3aa76e9b
 */

import { Vue, Component, Ref, Watch } from 'vue-property-decorator'

@Component
export default class Mixin extends Vue {
  testFun() {
    console.log('11111111111111111111')
  }
  mountedFun() {
    console.log('2222222222222222222')
  }
  created() {
    this.testFun()
  }
  mounted() {
    this.mountedFun()
  }
}
复制代码

 

posted @   JeckHui  阅读(1002)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-02-18 【前端】js截取or分割字符串的常见方法
点击右上角即可分享
微信分享提示