Vue Typescript data/method/computed的转变

news/2024/7/10 1:21:03 标签: vue
  1. data 以成员变量的形式存在
  2. method 以类的函数形式存在
  3. computed 使用 get 标识
<!--
 * @Author: your name
 * @Date: 2020-12-02 14:30:50
 * @LastEditTime: 2020-12-02 17:00:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hello-typescript\src\views\ForMixins.vue
-->
<template>
  <div>
    <h1>{{ mixinVal }}</h1>
    <!--证实私有属性可以混入-->
    <span>For Test whether private properties can be mixed: {{mixinPrivate}} </span><br/>
    <button @click="getCount">{{computedCount}}</button><br/>
    <!--证实方法可以混入-->
    <button @click="getMixinsCount">检测混入方法:{{mixinsCount}}</button>
  </div>
</template>
<script lang="ts">
import { Component, Watch } from 'vue-property-decorator'
import { mixins } from 'vue-class-component'
import GreetingWords from '@/mixins/GreetingWords.ts'
@Component
export default class ForMixins extends mixins(GreetingWords) {
  // data
  private count = 0 // 同名属性混入会报错
  public counts = 0
  protected counting = 0

  // methods
  public getCount () {
    return this.count++
  }

  // computed
  // computed检测的属性也是data属性,也属于成员变量
  get computedCount () {
    return this.count + this.mixinsCount
  }

  // watch 实现属性监听
  @Watch('count', { immediate: false, deep: false })
  onCountValueChange (val: number, oldValue: number) {
    if (val !== oldValue) {
      window.console.log('count number => ', val)
    }
  }

  mounted () {
    window.console.log('MixinsVal => ', this.mixinVal)
  }
}
</script>


http://www.niftyadmin.cn/n/1162487.html

相关文章

Vue Typescript @Watch

语法&#xff1a;Watch(path: string, options: WatchOptions {})参数说明&#xff1a;path: string类型 被侦听的属性名options&#xff1a;类型WatchOptions{}&#xff0c;可以包含两个属性immediate boolean 侦听开始之后是否立即调用该回调函数&#xff1b;deepboolean 被…

Vue Typescript @Model

定制prop和event 用作双向数据绑定 默认情况下, 一个组件上的v-model会:将 value用作 prop将 input用作 event语法&#xff1a; model: {prop?: string, event?: string} <!--* FilePath: \hello-typescript\src\components\Child.vue --> <template><div sty…

Vue Typescript @Prop

语法&#xff1a;Prop(options: (PropOptions | Constructor[] | Constructor) {})参数说明&#xff1a;Prop装饰器接收一个参数&#xff0c;这个参数可以有三种写法&#xff1a;Constructor例如String&#xff0c;Number&#xff0c;Boolean等&#xff0c;指定 prop 的类型&a…

vue + typescript 引入Vant-UI

安装 vant npm install vant --save1. 全局引入在 main.ts 中全局引入import vant/lib/vant-css/index.cssVue.use(Vant)2. 按需引入 两种按需引入方法&#xff0c;都可以实现按照需求引入&#xff0c;推荐第二种第一种&#xff1a;步骤一&#xff1a;在src下定义 plugins 目录…

vue 项目中常用的三种数据存储

vue 项目中常用的三种数据存储 1. Cookies 存储 介绍&#xff1a; 老技术、指定有效期、不能跨域&#xff08;浏览器&#xff0c;网站&#xff0c;路径&#xff09; cookie会跟随http协议发往后台&#xff0c;cookie最好处于服务器环境中使用 只能…

JS 序列化 与 反序列化

序列化 定义&#xff1a; 序列化是把对象转换成有序字节流&#xff0c;以便在网络上传输或者保存在本地文件中。 序列化机制的核心作用就是对象状态的保存与重建。 本质上讲&#xff0c;序列化就是把实体对象状态按照一定的格式写入到有序字节流 …

nodejs + express 发布vue项目

利用nodejs express 实现vue项目的快速发布 安装环境&#xff1a;nodejs : https://www.runoob.com/nodejs/nodejs-install-setup.html 菜鸟教程 很详细全面express: npm install express --save -gexpress-generator: npm install express-generator --save -g 创建项目…

windows下 Nginx 发布 Vue项目

安装环境&#xff1a;nginx: https://nginx.org/en/download.html 根据环境下载对应版本在指定目录解压即可使用。 打包Vue项目&#xff1a;npm run build 打包文件在dist目录下 部署项目&#xff1a;将dist目录下打包文件迁移至nginx html目录下 修改配置&#xff1a;根据实…