Vue常用方法汇总

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

Vue 方法及其详细说明

  1. data(): 用于定义组件的数据对象。数据对象可以包含任意类型的属性,如字符串、数字、布尔值、数组、对象等。
new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
})
  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. computed: 用于定义计算属性。计算属性是基于其他数据属性进行计算得到的,它们的结果会被缓存,只有在依赖的数据发生变化时才会重新计算。
export default {
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe'
    }
  },
  computed: {
    fullName() {
      return this.firstName + ' ' + this.lastName;
    }
  }
}
  1. watch: 用于监听数据的变化。当指定的数据发生变化时,会执行一个函数。可以使用 immediate 选项来指定是否立即执行回调函数。
export default {
  data() {
    return {
      message: 'Hello Vue!'
    }
  },
  watch: {
    message(newVal, oldVal) {
      console.log('message changed from', oldVal, 'to', newVal);
    }
  }
}
  1. mounted(): 在组件挂载到 DOM 后执行的生命周期钩子。可以在此处执行一些初始化操作,如获取数据、设置事件监听器等。
export default {
  mounted() {
    console.log('Component mounted');
  }
}
  1. beforeDestroy(): 在组件销毁之前执行的生命周期钩子。可以在此处执行一些清理操作,如取消事件监听器、清除定时器等。
export default {
  beforeDestroy() {
    console.log('Component about to be destroyed');
  }
}

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

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

页面渲染

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

  1. v-model: 双向数据绑定,将表单元素的值与Vue实例的数据属性进行绑定。
<input v-model="message" type="text">
  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. v-for: 列表渲染,用于在模板中渲染列表数据。
<ul>
  <li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
  1. v-on: 监听事件,用于监听DOM事件并在触发时执行相应的JavaScript代码。
<button v-on:click="increment">Increment</button>
  1. v-bind: 绑定属性,用于动态地绑定一个或多个属性到Vue实例的数据属性上。
<div v-bind:class="{ active: isActive }"></div>
  1. v-text: 更新元素的textContent。
<p v-text="message"></p>
  1. v-html: 更新元素的innerHTML。
<div v-html="content"></div>
  1. v-show: 根据表达式的值来切换元素的display属性。
<div v-show="isShown">This is shown if isShown is true.</div>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一花一world

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值