- Vue.js 2 Design Patterns and Best Practices
- Paul Halliday
- 167字
- 2021-06-24 18:33:06
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'
}
}
}
推薦閱讀
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 物聯(lián)網(wǎng)安全(原書第2版)
- 萬物互聯(lián):蜂窩物聯(lián)網(wǎng)組網(wǎng)技術(shù)詳解
- SD-WAN架構(gòu)與技術(shù)(第2版)
- PLC、現(xiàn)場總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- Bonita Open Solution 5.x Essentials
- VMware NSX網(wǎng)絡(luò)虛擬化入門
- 6G:面向2030年的移動通信
- 夢工廠之材質(zhì)N次方:Maya材質(zhì)手冊
- 網(wǎng)管第一課:網(wǎng)絡(luò)操作系統(tǒng)與配置管理
- 網(wǎng)絡(luò)AI+:2030后的未來網(wǎng)絡(luò)
- React Cookbook
- 人人都該都懂的互聯(lián)網(wǎng)思維