今回は、vue.js の親子コンポーネントでのパラメーターの受け渡しについて詳しく説明します。vue.js の親子コンポーネントでパラメーターを渡す場合の 注意事項 について説明します。 、見てみましょう。
1. 次のコードを使用して新しいcomponentA.vueコンポーネントを作成します。
store.js コードは次のとおりです:
1 2 3 4 5 6 7 8 9 | const STORAGE_KEY = 'todos-vue.js'
export default {
fetch(){
return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]' )
},
save(items){
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items));
}
}
|
ログイン後にコピー
App.vue コードは次のとおりです:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <template>
<p id= "app" >
<h1 v-text= "title" ></h1>
<input v-model= "newItem" v-on:keyup.enter= "addNew" />
<ul>
<li v- for = "item in items" v-bind: class = "{finished:item.isFinished}" v-on:click='toogleFinish(item)'>
{{item.label}}
</li>
</ul>
<!-- 使用组件,注意驼峰命名法转化成短线 -->
<!-- 向自组件传数据 -->
<Component-a msgfromfather='you die !'></Component-a>
</p>
</template>
<script>
import Store from './store'
import ComponentA from './components/componentA'
export default {
name: 'app' ,
data () {
return {
title: 'this is a todo list' ,
items:Store.fetch(),
newItem: ''
}
},
components:{
ComponentA
},
watch:{
items:{
handler(items){
Store.save(items)
},
deep:true
}
},
methods:{
toogleFinish(item){
item.isFinished = !item.isFinished
},
addNew(){
this.items.push({
label:this.newItem,
isFinished:false,
})
this.newItem = ''
}
}
}
</script>
<style>
#app {
font-family: 'Avenir' , Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.finished{
text-decoration: underline;
}
</style>
|
ログイン後にコピー
ComponentA.vue コードは次のとおりです:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <template>
<p class = "hello" >
<h1>{{msg}}</h1>
<h2>{{msgfromfather}}</h2>
<button v-on:click= "onClickMe" >Click!</button>
</p>
</template>
<<script>
export default {
data(){
return {
msg: 'Hello form component a'
}
},
props:[ 'msgfromfather' ],
methods:{
onClickMe(){
console.log(this.msgfromfather);
}
}
}
</script>
<style scoped>
</style>
|
ログイン後にコピー
この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、PHP 中国語 Web サイトの他の関連記事に注目してください。
推奨読書:
以上がvue.js の親子コンポーネントで渡すパラメーターの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。