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

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'
}
}
}
主站蜘蛛池模板: 习水县| 尚志市| 平原县| 油尖旺区| 纳雍县| 榕江县| 青神县| 瓮安县| 堆龙德庆县| 澎湖县| 三江| 楚雄市| 赫章县| 锡林浩特市| 鄄城县| 土默特左旗| 襄垣县| 越西县| 临清市| 南投市| 隆尧县| 温泉县| 婺源县| 屯留县| 金川县| 沭阳县| 河池市| 女性| 莱西市| 仪陇县| 临西县| 大洼县| 石城县| 田阳县| 澜沧| 康定县| 绥江县| 凤阳县| 钟山县| 嘉黎县| 平陆县|