首页 技术 正文
技术 2022年11月18日
0 收藏 511 点赞 2,448 浏览 1366 个字

vue英文官网推荐了一个叫vue-class-component的包,可以以class的模式写vue组件。vue-class-component(以下简称Component)带来了很多便利:

1.methods,钩子都可以直接写作class的方法

2.computed属性可以直接通过get来获得

3.初始化data可以声明为class的属性

4.其他的都可以放到Component装饰器里

举个小例子

@Component({
props: {
firstName: String,
lastName: String
},
components: {
'component-a': ComponentA
}
})
export class XXXX extends Vue {
firstName: string;
lastName: string; //初始data
middleName = 'middle'; //computed 属性
get fullName() {
return this.firstName + this.lastName;
} //method
hello() {
alert(`Hello ${this.fullName}!`);
} //钩子
mounted() {
this.hello();
}
}

现在尽管可以以class的模式来写vue的组件了,但自动补全,代码提示等功能还是没有,要想获取好的代码提示还得是原语言啊,js代码在.ts,.js文件写,scss在.scss写,html在.html写。

最终vue组件以以下方式写感觉挺爽,很顺

import Vue from 'vue';
import Componet from 'vue-class-component';require('./XXX.template.scss');@Component({
template: require('./XXX.template.html'),
props: {
firstName: String,
lastName: String
},
components: {
'component-a': ComponentA
}
})
export class XXXX extends Vue {
firstName: string;
lastName: string; //初始data
middleName = 'middle'; //computed 属性
get fullName() {
return this.firstName + this.lastName;
} //method
hello() {
alert(`Hello ${this.fullName}!`);
} //钩子
mounted() {
this.hello();
}
}

现在各个文件回归它的本职工作了,哈哈哈,不过现在打包时有点小问题,

[Vue warn]: You are using the runtime-only build of Vue where the template option is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

解决方法也很简单,在webpack配置文件里 加上

alias: {
'vue': 'vue/dist/vue.esm.js'
}

即可。好的,现在代码补全,语法提示什么功能都回来了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848