“Vue v-text 指令”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
第1行: 第1行:
1
+
https://www.bilibili.com/video/BV12J411m7MG/?p=7
 +
 
 +
 
 +
设置标签的文本值(textContent)<syntaxhighlight lang="html">
 +
<div id="app">
 +
<h2 v-text="message"></h2>
 +
</div>
 +
 
 +
var app = new Vue({
 +
  el:"#app",
 +
  data:{
 +
    message:"hello world"
 +
  }
 +
})
 +
</syntaxhighlight>
 +
上面的示例会替换 h2 标签内的全部内容
 +
 
 +
有时候我们希望只对标签内的部分文本内容进行替换:<syntaxhighlight lang="html">
 +
<div id="app">
 +
<h2>深圳{{ message }}</h2>
 +
</div>
 +
 
 +
var app = new Vue({
 +
  el:"#app",
 +
  data:{
 +
    message:"hello world"
 +
  }
 +
})
 +
</syntaxhighlight>上面示例中这种写法就是官方推荐的 <u>插值表达式</u>,可以只对标签内部的部分文本内容进行替换。
 +
 
 +
字符串拼接<syntaxhighlight lang="html">
 +
<div id="app">
 +
<h2 v-text="message+'!'"></h2>
 +
<h2>深圳{{ message + "!" }}</h2>
 +
</div>
 +
 
 +
var app = new Vue({
 +
  el:"#app",
 +
  data:{
 +
    message:"hello world"
 +
  }
 +
})
 +
</syntaxhighlight><syntaxhighlight lang="html">
 +
<!DOCTYPE html>
 +
<html lang="en">
 +
<head>
 +
    <meta charset="UTF-8">
 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 +
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
 +
    <title>v-text指令</title>
 +
</head>
 +
<body>
 +
  <div id="app">
 +
    <h2 v-text="message">北京</h2>
 +
    <h2 v-text="message+'!'">北京</h2>
 +
    <h2 v-text="info">北京</h2>
 +
    <h2 v-text="info+'!'">北京</h2>
 +
    <h2>{{ message }}北京</h2>
 +
    <h2>{{ message + '!' }}北京</h2>
 +
  </div>
 +
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
 +
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
 +
<script>
 +
    var app = new Vue({
 +
        el: "#app",
 +
        data:{
 +
            message:"hello world!!!",
 +
            info:"前端测试"
 +
        }
 +
    })
 +
</script>
 +
</body>
 +
</html>
 +
</syntaxhighlight>

2023年6月7日 (三) 08:28的版本

https://www.bilibili.com/video/BV12J411m7MG/?p=7


设置标签的文本值(textContent)

<div id="app">
 <h2 v-text="message"></h2>
</div>

var app = new Vue({
  el:"#app",
  data:{
    message:"hello world"
  }
})

上面的示例会替换 h2 标签内的全部内容

有时候我们希望只对标签内的部分文本内容进行替换:

<div id="app">
 <h2>深圳{{ message }}</h2>
</div>

var app = new Vue({
  el:"#app",
  data:{
    message:"hello world"
  }
})

上面示例中这种写法就是官方推荐的 插值表达式,可以只对标签内部的部分文本内容进行替换。 字符串拼接

<div id="app">
 <h2 v-text="message+'!'"></h2>
 <h2>深圳{{ message + "!" }}</h2>
</div>

var app = new Vue({
  el:"#app",
  data:{
    message:"hello world"
  }
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>v-text指令</title>
</head>
<body>
  <div id="app">
    <h2 v-text="message">北京</h2>
    <h2 v-text="message+'!'">北京</h2>
    <h2 v-text="info">北京</h2>
    <h2 v-text="info+'!'">北京</h2>
    <h2>{{ message }}北京</h2>
    <h2>{{ message + '!' }}北京</h2>
  </div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data:{
            message:"hello world!!!",
            info:"前端测试"
        }
    })
</script>
</body>
</html>