ChingFeng
Sep 2, 2018 · 3 min read

VueJs元件篇-component(五)

ref註冊狀態

將自己的狀態註冊到root,提供全域變數存取。

◉語法

<templement ref="refName"></templement>使用ref標籤進行註冊,透過$refs.refName.parament來存取變數。

◉應用

=【Html】=
— — — — — — — — — — — —
<div id="app">
<child ref="junior"></child>
<p>Trunth: {{childStomach}}</p>
</div>
— — — — — — — — — — — —
=【VueJs】=
— — — — — — — — — — — —
Vue.component(
'child',
{
template: "<p>{{mouth}}</p>",
data(){
return {
mouth: 'I didn`t eat that cookie',
stomach: 'Yummy that cookie was delicious.'
}
}
});
new Vue({
el:'#app',
data:{
childStomach: 'unknown'
},
mounted(){
this.childStomach = this.$refs.junior.stomach
}
});

◉進階應用

=【Html】=
— — — — — — — — — — — —
<div id="app">
<child v-for="i in 10" ref="junior" :num="i" :key="i"></child>
<p>Trunth for child 4: {{child4Stomach}}</p>
</div>
— — — — — — — — — — — —
=【VueJs】=
— — — — — — — — — — — —
Vue.component(
'child',
{
template: "<p>{{num}}: {{mouth}}</p>",
props: ['num'],
data(){
return {
mouth: 'I didn`t eat that cookie',
stomach: 'Yummy that cookie was '+ this.num+ ' times more delicious.'
}
}
});
new Vue({
el:'#app',
data:{
child4Stomach: 'unknown'
},
mounted(){
this.child4Stomach = this.$refs.junior[3].stomach
}
});

Object實戰

學習歷程紀錄

ChingFeng

Written by

叢林般的世界,一步步邁向未知的未來,迴盪迷途的工程師…

Object實戰

學習歷程紀錄

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade