Reputation: 7977
Here's a simple version of my application:
const app = Vue.createApp({ });
app.component('list', {
props: {
model: {
type: Object
}
},
template: `<div v-for="widget in model.widgets">
{{ widget.name }}
<div v-html="widget.content"></div>
<div v-html="widget.content2"></div>
<div v-html="widget.content3"></div>
</div>`
});
app.mount('#app');
I'm trying to pass the model as a property to my component, like so:
<div id="app">
<list :model="{
"widgets": [
{
"name": "Test",
"content": "<strong>Bold Text</strong>",
"content2": "<strong>Bold Text</strong>",
"content3": '<strong>Bold Text</strong>'
}
]
}"></list>
</div>
However, it doesn't display the text as bold and I'm not sure why.
Example snippet:
const app = Vue.createApp({});
app.component('list', {
props: {
model: {
type: Object
}
},
template: `<div v-for="widget in model.widgets">
{{ widget.name }}
<div v-html="widget.content"></div>
<div v-html="widget.content2"></div>
</div>`
});
app.mount('#app');
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<div id="app">
<list :model="{
"widgets": [
{
"name": "Test",
"content": "<strong>Bold Text</strong>",
"content2": "<strong>Bold Text</strong>"
}
]
}"></list>
</div>
Upvotes: 2
Views: 2296
Reputation: 8368
You don't need to use quotes as HTML entities, you can just structure it as normal JS:
const app = Vue.createApp({});
app.component('list', {
props: {
model: {
type: Object
}
},
template: `<div v-for="widget in model.widgets">
{{ widget.name }}
<div v-html="widget.content"></div>
<div v-html="widget.content2"></div>
</div>`
});
app.mount('#app');
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<div id="app">
<list :model="{
widgets: [
{
name: 'Test',
content: '<strong>Bold Text</strong>',
content2: '<strong>Bold Text</strong>'
}
]
}"></list>
</div>
Though, if for some reason you needed to use HTML entities, "
doesn't work because it's already enclosed in quotes and terminates the string, i.e. "Invalid "string""
.
If instead, you used single quotes, it would work as it's a valid string: "Valid 'string'"
. So you could use '
in it's place:
const app = Vue.createApp({});
app.component('list', {
props: {
model: {
type: Object
}
},
template: `<div v-for="widget in model.widgets">
{{ widget.name }}
<div v-html="widget.content"></div>
<div v-html="widget.content2"></div>
</div>`
});
app.mount('#app');
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<div id="app">
<list :model="{
'widgets': [
{
'name': 'Test',
'content': '<strong>Bold Text</strong>',
'content2': '<strong>Bold Text</strong>',
'content3': '<strong>Bold Text</strong>'
}
]
}"></list>
</div>
Upvotes: 1
Reputation: 2823
change your model data this way
:model="{
widgets:[
{
name: 'Test"',
content: '<strong>Bold Text</strong>',
content2: '<strong>Bold Text</strong>';
}
]
}"
Upvotes: 0