vue基础入门

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

一、vue是什么?

vue的两大特性:1.数据驱动视图 2. 双向数据绑定

1.1 数据驱动视图

在这里插入图片描述

2.2 双向数据绑定

在这里插入图片描述

2.3 MVVM

在这里插入图片描述
在这里插入图片描述

二、vue的基本使用

1.1 导入vue.js脚本

<!-- 在线导入 -->
<!-- 开发环境版本,包含了用帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 生产环境版本,优化了尺寸和速度 -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<!-- 本地导入 -->
<script src="node_modules/vue/dist/vue.js"></script>

1.2 在页面中声明一个将要被vue控制的DOM区域
1.3 创建vm实例对象
在这里插入图片描述

<body>
          <div id="root">{{username}}</div>

           <script>
                     const vm = new Vue({
                               el: '#root',
                               data: {
                                      username: 'duanrui' 
                               }
                     })
           </script>


</body>

三、Vue指令

1.1 内容渲染指令

在这里插入图片描述
v-text 指令会覆盖元素内部原有的内容(使用比较少)。
{{}}插值表达式:在实际开发中使用最多,只是内容上的占位符,不会覆盖原来的内容。
v-html:可以将带有标签的字符串渲染成真正的标签。

  <div id="root">{{username}}

                    <p v-text="gender">性别:</p>
          </div>

           <script>
                     const vm = new Vue({
                               el: '#root',
                               data: {
                                      username: 'duanrui',
                                      gender: '女'
                               }
                     })
           </script>

1.2 属性绑定

v-bind表示单向数据属性绑定,单向数据绑定可以简写成:

<body>
          <div id="root">
          <a v-bind:href="url">点击跳转</a>
          </div>

          <script>
                     const vm = new Vue({
                               el: '#root',
                               data: {
                                      username: 'duanrui',
                                      gender: '女',
                                      url:'http://www.baidu.com'
                               }
                     })
          </script>
          
</body>

1.3事件绑定

v-on 表示事件绑定可以简写成@click

<body>
          <div id="root">
          <h1>数量目前是:{{count}}</h1></h1>
          <button v-on:click="add">点击数量加1</button>
          </div>

          <script>
                     const vm = new Vue({
                               el: '#root',
                               data: {
                                      count:1
                               },
                               methods: {
                              add(){
                                        this.count++;
                              }
                                         
                               }
                     })
          </script>

绑定事件并传参
在绑定函数后面加()

<body>
          <div id="root">
                    <a v-bind:href="url">点击跳转</a>


                    <h1>数量目前是:{{count}}</h1>
                    </h1>
                    <button v-on:click="add">点击数量加1</button>
                    <button v-on:click="sub(2)">点击数量减1</button>
          </div>

          <script>
                    const vm = new Vue({
                              el: '#root',
                              data: {
                                        username: 'duanrui',
                                        gender: '女',
                                        url: 'http://www.baidu.com',
                                        count: 1
                              },
                              methods: {
                                        add() {
                                                  this.count++;
                                        },
                                        sub(n){
                                             this.count = this.count - n;     
                                        }
                                        
                              }
                    })
          </script>

</body>

vue中内置对象$event,可以获取到操作事件源

<body>
          <div id="root">
                    <a v-bind:href="url">点击跳转</a>


                    <h1>数量目前是:{{count}}</h1>
                    </h1>
                    <button v-on:click="even(2,$event)">检测even对象</button></button>
          </div>

          <script>
                    const vm = new Vue({
                              el: '#root',
                              data: {
                                        username: 'duanrui',
                                        gender: '女',
                                        url: 'http://www.baidu.com',
                                        count: 1
                              },
                              methods: {
                                        add() {
                                                  this.count++;
                                        },
                                        sub(n){
                                             this.count = this.count - n;     
                                        },
                                        even(n,e){
                                        console.log(e);
                                        }
                                        
                              }
                    })
          </script>

</body>

事件修饰符.prevent .stop

<a href="http://www.baidu.com" @click.prevent="show">事件修饰符</a>

 show(){
           console.log("链接被点击了一下!");
                                    }

在这里插入图片描述

1.4 按键修饰符

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>键盘事件</title>
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<div id="root">
			<h2>欢迎来到{{name}}学习</h2>
			<input type="text" placeholder="按下回车提示输入" @keydown.enter="showInfo">
		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false

		new Vue({
			el:'#root',
			data:{
				name:'尚硅谷'
			},
			methods: {
				showInfo(e){
					console.log(e.target.value)
				}
			},
		})
	</script>
</html>

1.5 双向数据绑定

v-model

   <select name="城市" id="root" v-model="city">
                    <option value="">请选择城市</option>
                    <option value="1">南京</option>
                    <option value="2">北京</option>
          </select>
          

          <script>
          new Vue({
                  el:'#root',
                  data:{
                          city:''
                   }
             })
          </script>

在这里插入图片描述

1.6 条件渲染指令

在这里插入图片描述
在这里插入图片描述

1.7 列表渲染

v-for 具体用法 v-for='item in list

<body>
          <div id="root">
                    <table>
                              <thead>
                                     <tr>
                                        <th style="width: 200px;">序列</th>
                                        <th style="width: 200px;">姓名</th>
                                        <th style="width: 200px;">身份证</th>   
                                     </tr>
                                        
                              </thead>
                              <tbody>
                                        <tr v-for='item in list'>
                                                  <td>{{item.id}}</td>
                                                  <td>{{item.name}}</td>
                                                  <td>{{item.idcard}}</td>
                                        </tr>
                              </tbody>
                    </table>
          </div>


          <script>
                      const vm = new Vue({
                               el: '#root',
                               data: {
                                      username: 'duanrui',
                                      gender: '女',
                                      list:[
                                                {id:1,name:'张三',idcard:'342626199607184713'},
                                                {id:2,name:'赵四',idcard:'342626199607184713'},
                                                {id:1,name:'王五',idcard:'342626199607184713'}
                                      ]
                               }
                     })

          </script>
          
</body>

在这里插入图片描述

Vue 过滤器

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值