<parent-component />
<child-component />
<grand-child-component />
$props
$props
$emit
$emit
<parent-component />
<child-component />
<grand-child-component />
Vuex $store
state
state
state
mutation
mutation
mutation
Vuex $store
<parent-component />
<child-component />
<grand-child-component />
$parent.knowledge
$parent.$parent.knowledge
<parent-component />
<grand-child-component />
$parent.$parent.knowledge?
<parent-component />
<child-component />
<grand-child-component />
$root.evil
<parent-component />
<child-component />
<grand-child-component />
$root.evil
<grand-parent-component />
<parent-component />
<child-component />
<grand-child-component />
provide
inject
<child-component />
<child-component />
<div>
<p>This is the count:</p>
<p>{{count}}</p>
<simple-inject/>
<button @click="count++"
class="button">
Increment!
</button>
</div>
SimpleProvide.vue
import SimpleInject from './SimpleInject';
export default {
name: 'simple-provide',
data(){
return {
count: 1,
};
},
provide(){
return {'count': this.count};
},
components: {
SimpleInject,
},
}
Template
Script
<div>
<p>This is the injected count:</p>
<p>{{count}}</p>
</div>
SimpleInject.vue
export default {
name: "simple-inject",
inject: ['count'],
}
Template
Script
<div>
<p>{{message}}</p>
<p>^ This is the message</p>
<p><input type="text"
v-model="message"></p>
<button class="button"
@click="buttonClick">
Modify Nested Component
</button>
<promise-inject/>
</div>
PromiseProvide.vue
import PromiseInject from "./PromiseInject"
export default {
data() {
return {
message: "Hello World",
}
},
methods: {
buttonClick() {
this.$emit('button-push', this.message)
}
},
components: {
PromiseInject
},
provide() {
const hp = (res,rej) =>{
this.$on('button-push', res)
}
return {
providedPromise: new Promise(hp),
}
}
}
Template
Script
<div>
<p>I'm a nested component!</p>
<p>{{childMessage}}</p>
</div>
PromiseInject.vue
export default {
name: "promise-inject",
data: function () {
return {
childMessage: 'I am waiting on a promise!'
}
},
methods: {
changeChildMessage(response) {
this.childMessage = response
}
},
inject: {
providedPromise:{
type:Promise,
}
},
mounted: function () {
this.providedPromise
.then(this.changeChildMessage)
}
}
Template
Script
<div>
<p>{{message}}</p>
<p>^ This is message</p>
<input type="text"
v-model="message">
<reactive-inject/>
</div>
ReactiveProvide.vue
import ReactiveInject from "./ReactiveInject"
export default {
data() {
return {
message: "Hello World",
}
},
provide() {
const get = ()=>this.message
const set = (v)=>this.message=v
return {
getParentMessage: get,
setParentMessage: set,
}
},
components: {
ReactiveInject
},
}
Template
Script
<div>
<p>I'm a nested component!</p>
<p>
<code>injectedMessage</code>
being reactive after injection:
</p>
<p>{{injectedMessage}}</p>
<p>
We can also set
<code>injectedMessage</code>
from this component
</p>
<p>
<input type="text"
class="input"
v-model="injectedMessage">
</p>
</div>
ReactiveInject.vue
export default {
name:"reactive-inject",
inject: [
"getParentMessage",
"setParentMessage",
],
computed: {
injectedMessage: {
get() {
return this.getParentMessage()
},
set(d) {
this.setParentMessage(d)
},
}
},
}
Template
Script
@fimion on twitter