Vue核心基础6:Vue内置指令、自定义指令、生命周期

1 Vue中的内置指令

javascript 复制代码
    <script>
        const vm = new Vue({
            el: '#root',
            data: {
                n: 1,
                m: 100,
                name: 'Vue',
                str: '<h3>你好</h3>'
            }
        })
    </script>

1.1 v-text

html 复制代码
<div v-text="name"></div>

1.2 v-html

html 复制代码
<div v-html="str"></div>

1.3 v-cloak

1.4 v-once

html 复制代码
<h2 v-once>初始化的n值: {{n}}</h2>
<h2>当前的n值: {{n}}</h2>
<button @click="n++">加1</button>

1.5 v-pre

html 复制代码
<h2 v-pre>Vue其实很简单的</h2>
<h2>当前的n值: {{m}}</h2>
<h2 v-pre>当前的n值: {{m}}</h2>
<button @click="m++">加1</button>

2 自定义指令

【代码】

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义指令</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- 需求1:定义一个v-big指令,和 v-text功能类似,但会把绑定的数值方法10倍 -->
    <!-- 需求2:定义一个v-fbind指令, 和v-bind功能类似,但可以让其绑定的input元素默认获得焦点-->

    <div id="root">
        <h2>当前的n值: <span v-text="n"></span></h2>
        <h2>放大十倍后的n值: <span v-big="n"></span></h2>
        <button @click="n++">加1</button>
        <hr>
        <!-- <input type="text" v-bind:value="n"> -->
        <input type="text" v-fbind:value="n">
    </div>
    <script>
            /*     // 定义全局指令
                Vue.directive('fbind', {
                    // 1.指令和元素成功绑定时(一上来的时候);
                    bind(element, binding) {
                        element.value = binding.value
                    },
                    // 2.指令所在元素被插入页面时调用
                    inserted(element, binding) {
                        element.focus()
                    },
                    // 3.指令所在模板被重新解析时调用
                    update(element, binding) {
                        // console.log('update')
                        element.value = binding.value
                    },
                })  */


        const vm = new Vue({
            el: '#root',
            data: {
                name: 'Vue',
                n: 1
            },

            // 所有指令相关的函数中的this都指向window
            directives: {
                // big函数何时会被调用
                // 1.指令和元素成功绑定时(一上来的时候);
                // 2.指令所在的模板被重新解析时;
                big(element, binding) {
                    element.innerHTML = binding.value * 10
                },
                fbind: {
                    // 1.指令和元素成功绑定时(一上来的时候);
                    bind(element, binding) {
                        element.value = binding.value
                    },
                    // 2.指令所在元素被插入页面时调用
                    inserted(element, binding) {
                        element.focus()
                    },
                    // 3.指令所在模板被重新解析时调用
                    update(element, binding) {
                        // console.log('update')
                        element.value = binding.value
                    },
                }
            }
        })
    </script>
</body>

</html>

3 生命周期

【代码】

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分析生命周期</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="root">
        <!-- <h2 :style="{opacity: opacity}">欢迎学习Vue</h2> -->
        <h2>当前的n值是: {{n}}</h2>
        <button @click="add">加1</button>

        <button @click="destory">点我销毁vm</button>
    </div>
    <script>
        const vm = new Vue({
            el: '#root',
            data: {
                n: 1,
            },
            methods: {
                add() {
                    this.n++
                },
                destory() {
                    console.log('销毁vm')
                    this.$destroy()
                }
            },
            beforeCreate() {
                console.log('beforeCreate', this)
                // debugger
            },
            created() {
                console.log('created', this)
            },
            beforeMount() {
                console.log('beforeMount', this)
            },
            mounted() {
                console.log('mounted', this)
            },
            beforeUpdate() {
                console.log('beforeUpdate', this)
            },
            updated() {
                console.log('updated', this)
            },
            beforeDestroy() {
                console.log('beforeDestroy', this)
            },
            destoryed() {
                console.log('destoryed', this)
            }


        })
    </script>
</body>

</html>

相关推荐
一路向前的月光2 分钟前
react(9)-redux
前端·javascript·react.js
我是哈哈hh1 小时前
【JavaScript进阶】作用域&解构&箭头函数
开发语言·前端·javascript·html
谢尔登1 小时前
Vue 和 React 响应式的区别
前端·vue.js·react.js
酷酷的阿云1 小时前
Vue3性能优化必杀技:useDebounce+useThrottle+useLazyLoad深度剖析
前端·javascript·vue.js
bin91531 小时前
DeepSeek 助力 Vue 开发:打造丝滑的缩略图列表(Thumbnail List)
前端·javascript·vue.js·ecmascript·deepseek
格式化小拓1 小时前
v-for的使用,遍历数组、对象、数字、字符串等类型
vue.js
鑫~阳2 小时前
Vue2是如何利用Object.defineProperty实现数据的双向绑定?
前端·vue.js·vue
圣心2 小时前
Ollama 快速入门
开发语言·javascript·人工智能
禾苗种树2 小时前
使用echart的dataZoom的labelFormatter自定义时间范围
前端·javascript·vue.js·echart
m0_748240912 小时前
SpringMVC 请求参数接收
前端·javascript·算法