How can we get compiled template work without .value
#7758
-
Hi all, I was wondering how I can make hand compiled template behave the same way as regular component. Thanks, Seb import { createApp, ref, h, compile } from "vue";
const CompOk = {
setup() {
const count = ref(0);
return { count };
},
template: `
<div>
<button @click="count++">+</button>
<button @click="count--">-</button>
{{ count }}
</div>
`,
};
const CompKo = {
setup() {
const count = ref(0);
const template = `
<div>
<button @click="count.value++">+</button>
<button @click="count.value--">-</button>
{{ count }}
</div>
`;
return () => h(compile(template), { count });
},
};
const App = {
components: { CompOk, CompKo },
template: `
<div style="display: flex; flex-direction: column;">
<comp-ok />
<comp-ko />
</div>
`,
};
createApp(App).mount("#app"); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
The point here is to be able to write the template section in the same fashion regardless on how it get handled (dynamically compiled vs standard vue template) |
Beta Was this translation helpful? Give feedback.
-
The difference is where you are rendering the template inside |
Beta Was this translation helpful? Give feedback.
-
Indeed, that is what I endup doing this weekend. Dynamically creating components and registering them to the app using the same setup function (returning the refs) and updating the Thanks for getting back to me. |
Beta Was this translation helpful? Give feedback.
The difference is where you are rendering the template inside
CompKo
's setup method, whereas theCompOk
setup is returning thecount
and then it will compile theref
variable without itsvalue
property to the template. if you want to fix this issue, you have to move thetemplate
from setup, outside of it inCompKo
https://vuejs.org/api/composition-api-setup.html#usage-with-render-functions