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>

相关推荐
张元清9 小时前
useEffect 之外:专门处理异步、深比较和 SSR 的 Effect Hook
前端·javascript·面试
XinZong9 小时前
OpenClaw 中最经典的 6 款skill,真正能进工作流的 skills
javascript·后端
XinZong10 小时前
2026 AI社交深度评测:InStreet 与 ClawReach 核心差异解析
javascript
Bug-制造者10 小时前
【Vue3 实战】全局错误处理体系搭建:实现业务与错误彻底解耦
前端·javascript·vue.js
竹林81811 小时前
从ethers.js迁移到Viem:我在DeFi Dashboard项目中踩过的坑与最终方案
javascript
zithern_juejin11 小时前
ES6——Promise
javascript
桜吹雪11 小时前
所有智能体架构(1):反思 (Reflection)
javascript·人工智能
前端那点事12 小时前
Vue3+TS 封装高复用 ECharts 通用组件,自适应+防抖+主题切换,开箱即用
前端·vue.js
前端那点事12 小时前
Vue3+TS动态路由终极方案|后端权限、刷新不丢、按钮权限、解决所有404BUG
前端·vue.js