Vue入门-1.Vue基本语法

1. Vue基本语法

1. Vue基本语法

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1.Vue基本语法title>
    
    <script src="https://unpkg.com/vue@3/dist/vue.global.js">script>
head>
<body>
    
    <div id="app">{{message}}div>
body>

<script>
    const hello = {
        // 指定数据源, 即MVVM中的Model
        data:function(){
            return{
                message:'Hello Vue'
            }
        }
    }

    const app = Vue.createApp(hello)
    app.mount('#app')
script>
html>

2.内容渲染指令

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js">script>
head>
<body>
    <div id="app">
        <p>name: {{username}}p>
        <p>sex: {{gender}}p>

        
        <p>{{desc}}p>

        
        <p v-html="desc">p>
    div>
body>
<script>
    const vm = {
        data:function(){
            return{
                username: "111",
                gender: "男",
                desc: '百度'
            }
        }
    }
    const app = Vue.createApp(vm)
    app.mount('#app')
script>
html>

3.属性绑定指令

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js">script>
head>
<body>
    <div id="app">
        <a :href="link">百度a>
        <input type="text" :placeholder="inputValue">
        <img :src="imgSrc" :style="{width:w}" alt="" >
    div>
body>
<script>
    const vm = {
        data: function(){
            return {
                link: "http://www.baidu.com",
                // 文本框的提示内容
                inputValue: '请输入内容',
                // 图片地址
                imgSrc: './image/11.jpg',
                w: '50px'
            }
        }
    }
    const app = Vue.createApp(vm)
    app.mount('#app')
script>
html>

4.使用JavaScript表达式

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="./js/vue.global.js">script>
head>
<body>
    <div id="app">
        <p>{{number + 1}}p>
        <p>{{ok? 'True':'False'}}p>
        <p>{{message.split('').reverse().join('')}}p>
        <p :id="'list-' + id">xxxp>
        <p>{{user.name}}p>
    div>
body>
<script>
    const vm={
        data:function(){
            return{
                number: 9,
                ok: false,
                message: 'ABC',
                id: 3,
                user:{
                    name:'yang',
                }
            }
        }
    }
    const app = Vue.createApp(vm)
    app.mount('#app')
script>
html>

5.事件绑定指令

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="./js/vue.global.js">script>
head>
<body>
    <div id="app">
        <h3>count: {{count}}h3>
        
        <button v-on:click="addCount">+1button>
        
        <button @click="count+=1">+1button>
    div>    

body>

<script>
    const vm = {
        data: function(){
            return {
                // 初始值为0
                count:0,
            }
        },
        // 定义方法
        methods: {
            // 点击按钮,  count自增1
            addCount(){
                this.count += 1
            }
        }
    }
    const app = Vue.createApp(vm)
    app.mount("#app")
script>
html>

6.条件渲染指令

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="./js/vue.global.js">script>
head>
<body>
    
    <div id="app">
        <button @click="flag = !flag">按钮button>
        
        
        <p v-if="flag">v-if控制p>
        <p v-show="flag">v-show控制p>

        <p v-if="type=='A'">优秀p>
        <p v-else-if="type=='B'">良好p>
        <p v-else>需要加强p>
    div>
body>
<script>
    const vm = {
        data: function(){
            return{
                flag: false,
                type: 'A'
            }
        }
    }
    const app = Vue.createApp(vm)
    app.mount("#app")
script>
html>

7.列表渲染指令

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="./js/vue.global.js">script>
head>
<body>
    <div id="app">
        
        
        
        <input type="text" v-model="name">
        <button @click="addUser">添加button>

        
        <ul>
            
            
            <li v-for="(user, index) in userlist" :key="user.id">
                <input type="checkbox">
                <div>name: {{user.name}}div>
            li>
        ul>
    div>
body>
<script>
    const vm = {
        data : function(){
            return{
                // 定义用户列表
                userlist:[
                    {id:1, name:'111'},
                    {id:2, name:'222'}
                ],
                // 添加的下一个用户的id
                nextId: 3,
                // 输入的用户名
                name: ''
            }
        },
        methods:{
            // 添加用户函数
            addUser(){
                this.userlist.unshift({id:this.nextId, name:this.name})
                this.name=''
                this.nextId += 1
            }
        }
    }

    const app = Vue.createApp(vm)
    app.mount("#app")
script>
html>

你可能感兴趣的:(Web,vue.js,前端)