前端

Vue —— 安装 与 插值语法、指令语法

2023-12-13  本文已影响0人  Sam小兄弟

作者:Sam


1. 简介

Vue是一个渐进式框架

这里介绍Vue的安装与基本使用案例

2. 安装

vue可以使用导入js的方式安装

也可以使用脚手架工具安装

2.1 导入js文件安装

  1. 下载js文件并导入
    vue的js文件分为开发版和生成版
    • 开发版中会有代码提示
    • 生成版中没有代码提示
<script src="js/vue.js"></script>
  1. 使用cdn在线导入
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

【注意】如果使用的是开发版js文件,页面console中会有使用开发者的提示,需要执行以下配置,来关闭开发版提示

Vue.config.productionTip=false

2.2 npm安装

去看《vue-脚手架基础》

2.3 脚手架工具安装

去看《vue-脚手架基础》

3. 安装Vue_Devtools

Vue_Devtools用于检测页面是否使用vue
是安装在chrome浏览器上的工具
安装时直接拖入扩展工具即可

下载地址
https://pan.samland.club/s/cobyoK2H5t6tqwy

【注意】如果不安装Vue_Devtools工具,在使用开发版js包的时候,会有安装工具的提示

4. 插值语法

4.1 语法简介

// 定义一个vue实例
new Vue({
    el: '#div1',
    data: {
        name: 'Sam',
        password: '123456',
        content: 'hello world'
    }
})
  1. el:是一个选择器,指向html页面中的元素
  2. data:用来保存数据
  3. 对于同一个dom元素,只有第一个vue实例会起作用
<div id='div1'>
    <h1>{{name}}</h1>
    <p>{{content.toUpperCase()}}
</div>

4.2 示例

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="icon" href="favicon.ico">
    <script src="js/vue.js"></script>
    <title>001</title>
</head>
<body>
<div class="test1" id="test1">
    <h3>Hello {{name}}</h3>
    <p>{{content.toUpperCase()}}</p>
</div>
</body>
<script>
    Vue.config.productionTip = false

    new Vue({
        el: '#test1',
        data: {
            name: "Sam",
            content: 'hello world'
        }
    })
</script>
</html>

5. 指令语法

5.1 语法简介

语法

<h3>{{school.name}}</h3>
<a v-bind:href="school.url">{{school.name.toUpperCase()}}</a>
Vue.config.productionTip = false

new Vue({
    el: '#test1',
    data: {
        name: "Sam",
        content: 'hello world',
        school:{
            name: 'SSPU',
            url: 'http://www.sspu.com'
        }
    }
})
<a :href="school.url">
<a :href="school.url.toUpperCase()">{{school.name}}</a>

5.2 示例

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="icon" href="favicon.ico">
    <script src="js/vue.js"></script>
    <title>001</title>
</head>
<body>
<div class="test1" id="test1">
    <h3>Hello {{name}}</h3>
    <p>{{content.toUpperCase()}}</p>

    <hr>
    <h3>{{school.name}}</h3>
    <a :href="school.url">{{school.name.toUpperCase()}}</a>
</div>
</body>
<script src="js/001.js"></script>
</html>
// 001.js
Vue.config.productionTip=false

new Vue({
    el: '#test1',
    data:{
        name: "Sam",
        content: 'hello world',
        school:{
            name: 'sspu',
            url: 'http://www.sspu.com'
        }
    }
})

6. 模板语法的本质

插值语法与指令语法称为模板语法

模板语法中调用的值,例如

<div id='test1'>
    <span>{{username}}</span>
</div>
const vm = new Vue({
    el: '#test1',
    data: {
        username: 'James'
    }
})

本质上是访问vm对象的属性
vue会自动将data中的kv,映射到vm对象中

上一篇 下一篇

猜你喜欢

热点阅读