这是我第一次制作 npm 包,我正在制作包的演示,我想放一个组件使用的示例。
当我将组件用法放入 pre 和 code 标记中时,如下所示
它显示此错误
Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <style>, as they will not be parsed.
这是我的代码(App.vue):
<template>
<pre>
<code>
<template>
<vlider
:id="'first'"
:vlider-data="slider"
:theme="'theme-dark'"
v-model="inputRange"
@click="vliderClick()"
>
<template> slot="bullet" slot-scope="bullet"
<label>{{ bullet.data.label }}</label>
<i
class="em"
:class="[`em-${bullet.data.extras.icon}`]"
></i>
<a target="_blank" :href="bullet.data.extras.learnMore">Learn more ?</a>
</template>
</vlider>
</template>
<script>
import Vlider from "vlider";
export default {
name: "app",
components: {
Vlider
},
data() {
return {
inputRange: null,
slider: [
{label: "Angry", color: "#ffc300", extras: { icon: 'angry', learnMore: 'http://localhost/'}},
{label: "Expressionless", color: "#ffb0fe", extras: { icon: 'expressionless', learnMore: 'http://localhost/'}},
{label: "Astonished", color: "#ff6bd6", extras: { icon: 'astonished', learnMore: 'http://localhost/'}},
{label: "Confounded", color: "#ff9d76", extras: { icon: 'confounded', learnMore: 'http://localhost/'}},
{label: "Okay?", color: "#51eaea", extras: { icon: 'face_with_raised_eyebrow', learnMore: 'http://localhost/'}},
{label: "Blush", color: "#fb3569", extras: { icon: 'blush', learnMore: 'http://localhost/'}}
]
};
},
watch: {
inputRange() {
console.log(this.inputRange)
}
},
methods: {
vliderClick() {
console.log(`clicked`)
}
}
};
</script>
<style>
import "vlider/src/sass/vlider.scss"
</style>
</code>
</pre>
</template>
<script>
import Vlider from "vlider";
...
</script>
我希望它像 HTML 中的普通标签一样工作。 我尝试下载一些代码块 npm 包,但仍然不起作用,我需要你们的帮助和建议,感谢您的帮助
最佳答案
v-pre指令应该告诉 Vue 不要编译模板的该部分,但如果其内容包含(例如)<script>
,Vue 似乎仍然会抛出相同的警告。标签。无论如何,它不会将其内容显示为原始 HTML。您需要将其提取到数据变量中,并且不使用 v-html
这里(与你想要的相反):
new Vue({
el: '#app',
data() {
return {
codeSample: `
<template>
<vlider
:id="'first'"
:vlider-data="slider"
:theme="'theme-dark'"
v-model="inputRange"
@click="vliderClick()"
>
<template> slot="bullet" slot-scope="bullet"
<label>{{ bullet.data.label }}</label>
<i
class="em"
:class="['em-\${bullet.data.extras.icon}']"
></i>
<a target="_blank" :href="bullet.data.extras.learnMore">Learn more ?</a>
</template>
</vlider>
</template>
<script>
import Vlider from "vlider";
export default {
name: "app",
components: {
Vlider
},
data() {
return {
inputRange: null,
slider: [
{label: "Angry", color: "#ffc300", extras: { icon: 'angry', learnMore: 'http://localhost/'}},
{label: "Expressionless", color: "#ffb0fe", extras: { icon: 'expressionless', learnMore: 'http://localhost/'}},
{label: "Astonished", color: "#ff6bd6", extras: { icon: 'astonished', learnMore: 'http://localhost/'}},
{label: "Confounded", color: "#ff9d76", extras: { icon: 'confounded', learnMore: 'http://localhost/'}},
{label: "Okay?", color: "#51eaea", extras: { icon: 'face_with_raised_eyebrow', learnMore: 'http://localhost/'}},
{label: "Blush", color: "#fb3569", extras: { icon: 'blush', learnMore: 'http://localhost/'}}
]
};
},
watch: {
inputRange() {
console.log(this.inputRange)
}
},
methods: {
vliderClick() {
console.log('clicked')
}
}
};
</\script>
<style>
import "vlider/src/sass/vlider.scss"
</style>
`
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<pre><code>{{codeSample}}</code></pre>
</div>
当然,在数据变量中嵌入大块 HTML 有点笨拙,并且需要对各种位和片段进行一些转义(例如示例中包含的 ${...}
和 </script>
标记)。如果通过 ajax 导入该 HTML 字符串或将其作为 webpack import 导入,可能会更容易维护。而不是直接将其嵌入到 data()
中就像我在这里所做的那样。
(如果您想要代码示例的语法着色,您可能还需要查看 vue-highlightjs;它也取决于将源代码放在组件数据变量中而不是内联在模板内。)
或者简单的方法
如果您愿意提前转义 html,您可以将其直接放入模板中,并使用 v-pre
告诉 Vue 忽略嵌入 html 中的任何 mustache 符号:
new Vue({
el: '#app'
});
<script src="https://unpkg.com/vue@latest/dist/vue.js"></script>
<div id="app">
<pre><code v-pre><script>... {{foo}} </script></code></pre>
</div>
关于javascript - 如何将Vue代码放入vue中的<code>标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750950/