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

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'
}
}
}
主站蜘蛛池模板: 赣榆县| 纳雍县| 武胜县| 宿迁市| 隆尧县| 鸡西市| 保德县| 邛崃市| 都安| 武乡县| 焉耆| 通道| 班戈县| 台安县| 宁都县| 三河市| 兰西县| 克拉玛依市| 伽师县| 黄骅市| 合肥市| 砀山县| 鄂托克旗| 黑龙江省| 平和县| 宁城县| 平利县| 南宁市| 武城县| 越西县| 西贡区| 小金县| 新泰市| 孝义市| 安康市| 水富县| 商丘市| 青州市| 娄烦县| 津市市| 图片|