Vue - 从字符串中渲染一个元素 [英] Vue - Render an element out of string
问题描述
我想从我的数据库中的字符串创建一个 vue 元素.
I would like to create a vue element from a string from my database.
在这种情况下,它应该是带有笑脸表情符号的消息.我实际上将它保存为:Some text with Emoji: :santa::skin-tone-3:
,并用 <Emoji emoji=' 替换 '::' 之间的所有有效字符串:santa::skin-tone-3:' :size='16'/>
In this case, it should be a message with a smiley emoji.
I actually save it like: Some text with Emoji: :santa::skin-tone-3:
, and replace all valid string between '::' with the <Emoji emoji=':santa::skin-tone-3:' :size='16' />
<template>
<span class=message v-html=convertedMessage></div>
</template>
<script>
import { Emoji } from 'emoji-mart-vue'
export default {
components: {
Emoji
},
computed:{
convertedMessage(){
return "Some text with Emoji: "+"<Emoji emoji=':santa::skin-tone-3:' :size='16' />"
}
}
}
</script>
但不是呈现的元素,它应该是这样的:
But instead of the rendered element which should be something like:
<span data-v-7f853594="" style="display: inline-block; width: 32px; height: 32px; background-image: url("https://unpkg.com/emoji-datasource-apple@4.0.4/img/apple/sheets/64.png"); background-size: 5200%; background-position: 15.6863% 41.1765%;"></span>
我只得到:
<emoji emoji=":santa::skin-tone-3:" :size="16"></emoji>
按照预期渲染此元素的最佳可能性是什么?
What is the best possibility to render this Element like intended?
推荐答案
这里有一些更简单的方法来完成您通常想要的操作.如果您提供更多细节,您的正确方向可能是这些解决方案之一之前的策略模式,但这些解决方案之一可能是您想要的:
Here are some much easier ways to do what you generally want. If you give more specifics, your right direction may be a strategy pattern before one of these solutions, but one of these solutions is probably what you want:
1) Vue 让你可以动态定义开箱即用的组件,所以这行代码:
1) Vue lets you dynamically define components right out of the box, so this single line:
<component v-for="(component, index) in components" :key="'component'+index" :is="component.name" v-bind="component.props" />
...会在这样的对象数组中绘制一堆组件(例如):{name: 'myComponentName', props: {foo: 1, bar: 'baz'}}.
...would draw a bunch of components in an array of objects like this (for example): {name: 'myComponentName', props: {foo: 1, bar: 'baz'}}.
2) Vue 允许您通过简单地添加 v-html="variable"
2) Vue lets you inject HTML into components by simply adding v-html="variable"
例如,这里有一个创建动态 SVG 图标的组件,其中 SVG 的内容是从 JavaScript 变量动态注入的...
For example, here is a component that creates dynamic SVG icons, where the contents of the SVG is dynamically injected from JavaScript variables...
<template>
<svg xmlns="http://www.w3.org/2000/svg"
:width="width"
:height="height"
viewBox="0 0 18 18"
:aria-labelledby="name"
role="presentation"
>
<title :id="name" lang="en">{{name}} icon</title>
<g :fill="color" v-html="path">
</g>
</svg>
</template>
<script>
import icons from '../common/icons'
export default {
props: {
name: {
type: String,
default: 'box'
},
width: {
type: [Number, String],
default: 18
},
height: {
type: [Number, String],
default: 18
},
color: {
type: String,
default: 'currentColor'
}
},
data () {
return {
path: icons[this.name]
}
},
created () {
console.log(icons)
}
}
</script>
<style scoped>
svg {
display: inline-block;
vertical-align: baseline;
margin-bottom: -2px;
}
</style>
3) Vue 让你通过 this.$options.template 动态定义你的组件模板:
3) Vue lets you dynamically define your component template through this.$options.template:
export default {
props: ['name', 'props'],
template: '',
created(){
this.$options.template = `<component :is="name" ${props.join(' ')} ></component>`
},
}
4) Vue 允许您定义渲染函数,因此代理组件或其他高级恶作剧是微不足道的:
4) Vue lets you define a render function, so proxy components or other advanced shenanigans are trivial:
Vue.component('component-proxy', {
props: {
name: {
type: String,
required: true
},
props: {
type: Object,
default: () => {}
}
},
render(h) {
// Note the h function can render anything, like h('div') works too.
// the JS object that follows can contain anything like on, class, or more elements
return h(this.name, {
attrs: this.props
});
}
});
一个聪明的天才在这里为此编写了一个 jsbin:http://jsbin.com/fifatod/5/edit?html,js,output
A smart genius wrote a jsbin for this here: http://jsbin.com/fifatod/5/edit?html,js,output
5) Vue 允许您使用 Vue.extend 创建组件,甚至将原始 JavaScript 对象传递到页面或应用程序组件部分,就像这样,它从模板的简单字符串和一个名为foo"的组件创建一个名为foo"的组件用于 props 的数组,您还可以单独使用 JS 对象以相同的方式扩展数据、创建、打开等:
5) Vue allows you to create components with Vue.extend or even passing in raw JavaScript objects into a page or apps components section, like this, which creates a component named "foo" from a simple string for the template and an array for props, you could also extend the data, created, on, etc. the same way using the JS object alone:
new Vue({
el: '#app',
data: {
foo: 'bar',
props: {a: 'a', b: 'b'}
},
components: {
foo: {
template: '<p>{{ a }} {{ b }}</p>',
props: ['a', 'b']
}
}
})
这篇关于Vue - 从字符串中渲染一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!