• Vue常用方法汇总


    Vue.js 是一个用于构建用户界面的渐进式框架。以下是一些常用的 Vue 方法及其详细说明和代码示例:

    Vue 方法及其详细说明

    1. data(): 用于定义组件的数据对象。数据对象可以包含任意类型的属性,如字符串、数字、布尔值、数组、对象等。
    new Vue({
      el: '#app',
      data: {
        message: 'Hello Vue!'
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. methods: 用于定义组件的方法。方法可以包含任意数量的参数,并在模板中通过 v-on 指令或简写为 @ 来调用。
    <template>
      <div>
        <p>{{ message }}p>
        <button @click="showMessage">点击显示消息button>
      div>
    template>
    
    <script>
    export default {
      data() {
        return {
          message: 'Hello Vue!'
        }
      },
      methods: {
        showMessage() {
          alert(this.message);
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    1. computed: 用于定义计算属性。计算属性是基于其他数据属性进行计算得到的,它们的结果会被缓存,只有在依赖的数据发生变化时才会重新计算。
    export default {
      data() {
        return {
          firstName: 'John',
          lastName: 'Doe'
        }
      },
      computed: {
        fullName() {
          return this.firstName + ' ' + this.lastName;
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. watch: 用于监听数据的变化。当指定的数据发生变化时,会执行一个函数。可以使用 immediate 选项来指定是否立即执行回调函数。
    export default {
      data() {
        return {
          message: 'Hello Vue!'
        }
      },
      watch: {
        message(newVal, oldVal) {
          console.log('message changed from', oldVal, 'to', newVal);
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. mounted(): 在组件挂载到 DOM 后执行的生命周期钩子。可以在此处执行一些初始化操作,如获取数据、设置事件监听器等。
    export default {
      mounted() {
        console.log('Component mounted');
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. beforeDestroy(): 在组件销毁之前执行的生命周期钩子。可以在此处执行一些清理操作,如取消事件监听器、清除定时器等。
    export default {
      beforeDestroy() {
        console.log('Component about to be destroyed');
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Vue.js 是一个用于构建用户界面的渐进式框架。以下是一些常用的 Vue 方法及其详细说明和代码示例:

    1. new Vue(): 创建一个新的 Vue 实例。
    const app = new Vue({
      el: '#app',
      data: {
        message: 'Hello Vue!'
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. data: 定义组件的数据对象。
    data() {
      return {
        message: 'Hello Vue!'
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. methods: 定义组件的方法。
    methods: {
      showMessage() {
        alert(this.message);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. computed: 定义计算属性。
    computed: {
      reversedMessage() {
        return this.message.split('').reverse().join('');
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. watch: 监听数据的变化。
    watch: {
      message(newVal, oldVal) {
        console.log('message changed from', oldVal, 'to', newVal);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. mounted: 在组件挂载到 DOM 后执行的生命周期钩子。
    mounted() {
      console.log('Component mounted');
    }
    
    • 1
    • 2
    • 3
    1. beforeDestroy: 在组件销毁之前执行的生命周期钩子。
    beforeDestroy() {
      console.log('Component about to be destroyed');
    }
    
    • 1
    • 2
    • 3
    1. created: 在组件创建完成后立即执行的生命周期钩子。
    created() {
      console.log('Component created');
    }
    
    • 1
    • 2
    • 3
    1. updated: 在组件更新时执行的生命周期钩子。
    updated() {
      console.log('Component updated');
    }
    
    • 1
    • 2
    • 3
    1. destroyed: 在组件销毁时执行的生命周期钩子。
    destroyed() {
      console.log('Component destroyed');
    }
    
    • 1
    • 2
    • 3
    1. props: 定义组件的属性。
    props: {
      message: String
    }
    
    • 1
    • 2
    • 3
    1. components: 注册全局组件。
    components: {
      MyComponent: { /* ... */ }
    }
    
    • 1
    • 2
    • 3
    1. directives: 注册自定义指令。
    directives: {
      focus: { /* ... */ }
    }
    
    • 1
    • 2
    • 3
    1. filters: 注册自定义过滤器。
    filters: {
      reverse: function (value) {
        return value.split('').reverse().join('');
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. mixins: 混入其他选项。
    mixins: [/* ... */]
    
    • 1
    1. provide: 向子组件提供数据。
    provide() {
      return {
        message: 'Hello from parent component'
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. inject: 从父组件接收数据。
    inject: ['message']
    
    • 1
    1. ref: 创建一个响应式的引用。
    const myRef = ref('Hello Vue!')
    
    • 1
    1. nextTick: 在下一个 DOM 更新周期之后执行回调函数。
    nextTick(() => {
      console.log('This will run after the next DOM update cycle');
    })
    
    • 1
    • 2
    • 3
    1. onMounted: 在组件挂载到 DOM 后立即执行的生命周期钩子。
    onMounted(() => {
      console.log('Component mounted');
    })
    
    • 1
    • 2
    • 3
    1. onUpdated: 在组件更新时执行的生命周期钩子。
    onUpdated(() => {
      console.log('Component updated');
    })
    
    • 1
    • 2
    • 3
    1. onBeforeUpdate: 在组件更新前执行的生命周期钩子。
    onBeforeUpdate(() => {
      console.log('Component about to update');
    })
    
    • 1
    • 2
    • 3
    1. onUnmounted: 在组件卸载时执行的生命周期钩子。
    onUnmounted(() => {
      console.log('Component unmounted');
    })
    
    • 1
    • 2
    • 3
    1. onActivated: 在组件被激活时执行的生命周期钩子。
    onActivated(() => {
      console.log('Component activated');
    })
    
    • 1
    • 2
    • 3
    1. onDeactivated: 在组件被停用时执行的生命周期钩子。
    onDeactivated(() => {
      console.log('Component deactivated');
    })
    
    • 1
    • 2
    • 3
    1. onErrorCaptured: 捕获错误并处理。
    onErrorCaptured(error, instance, info) {
      console.log('Error captured:', error);
    }
    
    • 1
    • 2
    • 3
    1. onSuspended: 在组件暂停时执行的生命周期钩子。
    onSuspended(() => {
      console.log('Component suspended');
    })
    
    • 1
    • 2
    • 3
    1. onReactivated: 在组件恢复时执行的生命周期钩子。
    onReactivated(() => {
      console.log('Component reactivated');
    })
    
    • 1
    • 2
    • 3
    1. onRendered: 在组件渲染完成后执行的生命周期钩子。
    onRendered(() => {
      console.log('Component rendered');
    })
    
    • 1
    • 2
    • 3
    1. onDestroyed: 在组件销毁时执行的生命周期钩子。
    onDestroyed(() => {
      console.log('Component destroyed');
    })
    
    • 1
    • 2
    • 3

    页面渲染

    Vue.js是一个流行的JavaScript框架,用于构建用户界面。以下是一些Vue.js的常用方法和代码示例:

    1. v-model: 双向数据绑定,将表单元素的值与Vue实例的数据属性进行绑定。
    <input v-model="message" type="text">
    
    • 1
    1. v-ifv-else-ifv-else: 条件渲染,根据条件判断来决定是否渲染元素。
    <div v-if="isShown">
      <p>This is shown if isShown is true.p>
    div>
    <div v-else-if="isHidden">
      <p>This is shown if isHidden is true and isShown is false.p>
    div>
    <div v-else>
      <p>This is shown if isShown and isHidden are false.p>
    div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. v-for: 列表渲染,用于在模板中渲染列表数据。
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}li>
    ul>
    
    • 1
    • 2
    • 3
    1. v-on: 监听事件,用于监听DOM事件并在触发时执行相应的JavaScript代码。
    <button v-on:click="increment">Incrementbutton>
    
    • 1
    1. v-bind: 绑定属性,用于动态地绑定一个或多个属性到Vue实例的数据属性上。
    <div v-bind:class="{ active: isActive }">div>
    
    • 1
    1. v-text: 更新元素的textContent。
    <p v-text="message">p>
    
    • 1
    1. v-html: 更新元素的innerHTML。
    <div v-html="content">div>
    
    • 1
    1. v-show: 根据表达式的值来切换元素的display属性。
    <div v-show="isShown">This is shown if isShown is true.div>
    
    • 1
  • 相关阅读:
    Prometheus-Rules 实战
    攻防世界-web-FlatScience
    【前端】CSS:border
    大佬公司的github地址
    2023年11月11日~11月17日周报(基于matlab生成模拟数据、批量修改文件名、重写dataset)
    mac苹果电脑有什么免费的系统清理软件?
    银行双活数据中心建设项目实践
    Kotlin开发者眼中的Java缺少哪些特性?
    无胁科技-TVD每日漏洞情报-2022-9-20
    学习笔记 | 音视频 | 推流项目框架及细节
  • 原文地址:https://blog.csdn.net/ACCPluzhiqi/article/details/134322681