Vue - v-html 不显示为 HTML
Vue - v-html not displaying as HTML
这是我的应用程序的简单版本:
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');
我正在尝试将模型作为 属性 传递给我的组件,如下所示:
<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>
但是,它没有将文本显示为粗体,我不确定为什么。
示例片段:
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/vue@3.0.2/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>
您不需要将引号用作 HTML 实体,您可以像普通 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/vue@3.0.2/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>
但是,如果出于某种原因您需要使用 HTML 个实体,"
将不起作用,因为它已经用引号括起来并终止了字符串,即 "Invalid "string""
。
如果相反,您使用单引号,它将作为有效字符串工作:"Valid 'string'"
。所以你可以在它的位置使用 '
:
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/vue@3.0.2/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>
以这种方式更改您的模型数据
:model="{
widgets:[
{
name: 'Test"',
content: '<strong>Bold Text</strong>',
content2: '<strong>Bold Text</strong>';
}
]
}"
这是我的应用程序的简单版本:
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');
我正在尝试将模型作为 属性 传递给我的组件,如下所示:
<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>
但是,它没有将文本显示为粗体,我不确定为什么。
示例片段:
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/vue@3.0.2/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>
您不需要将引号用作 HTML 实体,您可以像普通 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/vue@3.0.2/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>
但是,如果出于某种原因您需要使用 HTML 个实体,"
将不起作用,因为它已经用引号括起来并终止了字符串,即 "Invalid "string""
。
如果相反,您使用单引号,它将作为有效字符串工作:"Valid 'string'"
。所以你可以在它的位置使用 '
:
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/vue@3.0.2/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>
以这种方式更改您的模型数据
:model="{
widgets:[
{
name: 'Test"',
content: '<strong>Bold Text</strong>',
content2: '<strong>Bold Text</strong>';
}
]
}"