官术网_书友最值得收藏!

Properties

We've seen how we can create classes and use the Component decorator; let's now take a look at how we can define "props" inside of our class using the vue-property-decorator:

# Install Vue Property Decorator
npm install vue-property-decorator --save-dev

This depends on the vue-class-component, so anytime we install vue-property-decorator you'll need to ensure vue-class-component is also installed. Let's then define a Component property using the @Prop decorator:

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

// Omitted
@Component({
})
export default class App extends Vue {
@Prop({ default: 'Paul Halliday' }) name: string;
}
</script>

Notice how we're now importing the Component from 'vue-property-decorator' instead of vue-class-component. This is because it exports this as a module for us to import. We're then defining a new component property with the key of name and the default value of 'Paul Halliday'; prior to using TypeScript, it would have looked as follows:

export default {
props: {
name: {
type: String,
default: 'Paul Halliday'
}
}
}
主站蜘蛛池模板: 玛曲县| 江安县| 桃园市| 诸城市| 景德镇市| 蓬莱市| 顺义区| 富蕴县| 沅陵县| 和田市| 全椒县| 天津市| 西丰县| 东阳市| 安阳县| 孟州市| 安宁市| 麦盖提县| 遂宁市| 定西市| 林西县| 介休市| 安徽省| 沧州市| 乌鲁木齐县| 万安县| 灵石县| 福海县| 新泰市| 靖宇县| 屯留县| 县级市| 鱼台县| 平泉县| 凉城县| 乌兰县| 炎陵县| 西青区| 家居| 吉林市| 岫岩|