js
// 父级组件提供 'foo'
var Provider = {
: {
provide: "bar",
foo
},// ...
};
// 子组件注入 'foo'
var Child = {
: ["foo"],
injectcreated() {
console.log(this.foo); // => "bar"
},// ...
};
Vue3: Provide / inject | Vue.js
js
const app = Vue.createApp({});
app.component("todo-list", {
data() {
return {
: ["Feed a cat", "Buy tickets"],
todos
};
},: {
provide: "John Doe",
user
},: `
template <div>
{{ todos.length }}
<!-- rest of the template -->
</div>
`,
});
app.component("todo-list-statistics", {
: ["user"],
injectcreated() {
console.log(`Injected property: ${this.user}`); // > Injected property: John Doe
},
});