javascript - 按钮无法在 Vue 中应用模板语法
问题描述
我对我的 Vue 组件中的代码感到非常困惑,因为任何元素都从{{ title }}
一个按钮应用模板,该按钮仅将模板语法显示为纯 HTML。
我的代码:
<template>
<li>
<h2>{{ friend.name }}</h2>
<!-- this button has the {{ title }} in it, but vue wont render it when generating the page -->
<button>{{ title }}</button>
<!-- this p-tag on the other hand got the same syntax template and works just fint -->
<p>{{ title }}</p>
<ul v-if="detailsAreVisible">
<li><strong>Phone: </strong>{{ friend.phone }}</li>
<li><strong>Email: </strong>{{ friend.email }}</li>
</ul>
</li>
</template>
<script>
export default {
data() {
return {
//this the string that should be inside the button
title: "HELLO",
detailsAreVisible: false,
friend: {
id: "max",
name: "Max Musterman",
phone: "0151 2548 425",
email: "max@localhost.de",
},
};
},
methods: {
toggleDetails() {
this.detailsAreVisible = !this.detailsAreVisible;
},
},
computed: {
toggleButtonText() {
let innerText = this.detailsAreVisible ? "Hide" : "Show";
return innerText + " Details";
},
},
};
</script>
解决方案
当您复制过去 {{ title }} 时dot
,括号中的一个也过去了,这就是它不显示的原因。
const app = new Vue({
data() {
return {
//this the string that should be inside the button
title: "HELLO",
detailsAreVisible: false,
friend: {
id: "max",
name: "Max Musterman",
phone: "0151 2548 425",
email: "max@localhost.de",
},
};
},
})
app.$mount("#app")
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<li>
<h2>{{ friend.name }}</h2>
<!-- this button has the {{ title }} in it, but vue wont render it when generating the page -->
<button>{{ title }}</button>
<!-- this p-tag on the other hand got the same syntax template and works just fint -->
<p>{{ title }}</p>
<ul v-if="detailsAreVisible">
<li><strong>Phone: </strong>{{ friend.phone }}</li>
<li><strong>Email: </strong>{{ friend.email }}</li>
</ul>
</li>
</div>
推荐阅读
- linux - Cronjob 适用于普通用户,但不适用于 sudo
- angular - 为什么我们使用 - ngAfterContentInit 生命周期方法
- r - 从 R 中 writeClipboard 函数的输出中删除最后一个换行符
- python - Pytest 失败并显示 AssertionError False 是 False
- python - 在 Python 中使用 with 语句时,将 stderr 和 stdout 重定向到文件不会写入错误
- javascript - 当聚合函数为空时,Sequelize 获取结果
- python - Related_name 从 Django 表单返回属性错误 (NoneType)。我正在尝试在表单中创建一个动态过滤器
- c++ - 在“0”参数上强制编译错误(C++)
- node.js - 数组属性上的“updateMany”和“$pull”问题
- checkbox - 通过 JSXGraph 中的复选框可见的元素:更改其他参数?