第2节:vue3 模版语法

发布时间 2023-06-30 16:04:02作者: wuzexin

1、将vue-demo项目清理为空

(1)index.html 不动

 (2)main.js删除样式引用

(3)App.vue只保留template和script

(4)清空文件夹assets和components

 

2、模版语法

(1)文本插值

打开App.vue写一下代码

 

 1 <template>
 2 <h1>1、模版语法</h1>
 3 <h3>(1)、文本插值</h3>
 4  <p>{{ msg }}</p>
 5  <p>文本:{{ hello }}</p>
 6  <p>加法:{{ num+1 }}</p>
 7  <p>三目运算符:{{ ok?'Yes':'No'}}</p>
 8  <p>文本翻转(字符串转为数组,翻转,合并成字符串):{{ message.split('').reverse().join('')}}</p>
 9  
10 </template>
11 
12 <script>
13 export default{
14   data(){
15     return{
16       msg:"神器的语法",
17       hello:"Hello World!",
18       num:10,
19       ok:true,
20       message:"大家好!"
21     }
22   }
23 }
24 </script>

 

打开浏览器,页面显示效果

 

文本插值允许放入有返回值结果的,有return结果。

 (2)原始Html

 在App.vue中编辑

打开浏览器,页面显示效果

 

(3)属性绑定

代码示例:App.vue

 1 <template>
 2 <h1>1、模版语法</h1>
 3 <h3>(3)、属性绑定</h3>
 4 <div v-bind:id="dynamicId" v-bind:class="dynamicClass" v-bind:title="dynamicTitle" v-bind:about="dynamicAbout">测试</div>
 5 <p v-html="rawHtml"></p>
 6 </template>
 7 
 8 <script>
 9 export default{
10   data(){
11     return{
12       dynamicId:"appid",
13       dynamicClass:"appclass",
14       dynamicTitle:"测试",
15       dynamicAbout:null  //绑定的值是 null 或者 undefined,那么该属性将会从渲染的元素上移除
16     }
17   }
18 }
19 </script>
20 <style>
21 .appclass{
22   color:red;
23   font-size: 30px;
24 }
25 </style>

 

浏览器页面显示结果:

 (4)布尔型属性

 1 <template>
 2 <h1>1、模版语法</h1>
 3 <h3>(4)、布尔型属性</h3>
 4 <button disabled>button1</button><br>
 5 <button :disabled="isButtonDisabled">button2</button><br>
 6 <button :disabled="isButtonDisabled3">button3</button>
 7 </template>
 8 
 9 <script>
10 export default{
11   data(){
12     return{
13       isButtonDisabled:true,
14       isButtonDisabled3:false
15     }
16   }
17 }
18 </script>

 

页面浏览效果及显示代码元素

 

(5)动态绑定多个属性值

App.vue代码

 1 <template>
 2 <h1>1、模版语法</h1>
 3 <h3>(5)、动态绑定多个值</h3>
 4 <div v-bind="objectOfAttrs">测试</div>
 5 </template>
 6 
 7 <script>
 8 export default{
 9   data(){
10     return{
11       objectOfAttrs:{
12         id:"appid",
13         class:"appclass"
14       }
15     }
16   }
17 }
18 </script>
19 <style>
20 .appclass{
21   color:red;
22   font-size: 30px;
23 }
24 </style>

打开浏览器显示页面和元素