开心档之 Vue 教程 2

简介: 计算属性关键词: computed。计算属性在处理一些复杂逻辑时是很有用的。可以看下以下反转字符串的例子:

目录

Vue.js 计算属性

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#vuejs-%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7

实例 1

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-1

实例 2

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-2

computed vs methods

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#computed-vs-methods

实例 3

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-3

computed setter

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#computed-setter

实例 4

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-4

Vue.js 自定义指令

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#vuejs-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%87%E4%BB%A4

实例

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B

实例

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-1

钩子

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E9%92%A9%E5%AD%90

钩子函数

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0

钩子函数参数

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0

实例

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-2

实例

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E5%AE%9E%E4%BE%8B-3

Vue.js 目录结构

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#vuejs-%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84

目录解析

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#%E7%9B%AE%E5%BD%95%E8%A7%A3%E6%9E%90

src/APP.vue

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#srcappvue

src/components/Hello.vue

https://xie.infoq.cn/article/00e7dbb42ed8d0412c0a90498#srccomponentshellovue

编辑

计算属性关键词: computed。

计算属性在处理一些复杂逻辑时是很有用的。

可以看下以下反转字符串的例子:

实例 1

http://kxdang.com/topic/vue2/vue-computed.html#%E5%AE%9E%E4%BE%8B-1

<div id="app">
  {{ message.split('').reverse().join('') }}
</div>

实例 1 中模板变的很复杂起来,也不容易看懂理解。

接下来我们看看使用了计算属性的实例:

实例 2

http://kxdang.com/topic/vue2/vue-computed.html#%E5%AE%9E%E4%BE%8B-2

<div id="app">
  <p>原始字符串: {{ message }}</p>
  <p>计算后反转字符串: {{ reversedMessage }}</p>
</div>
<script>
var vm = new Vue({
  el: '#app',
  data: {
    message: 'Runoob!'
  },
  computed: {
    // 计算属性的 getter
    reversedMessage: function () {
      // `this` 指向 vm 实例
      return this.message.split('').reverse().join('')
    }
  }
})
</script>

实例 2 中声明了一个计算属性 reversedMessage 。

提供的函数将用作属性 vm.reversedMessage 的 getter 。

vm.reversedMessage 依赖于 vm.message,在 vm.message 发生改变时,vm.reversedMessage 也会更新。

computed vs methods

http://kxdang.com/topic/vue2/vue-computed.html#computed-vs-

我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。

实例 3

http://kxdang.com/topic/vue2/vue-

methods: {
  reversedMessage2: function () {
    return this.message.split('').reverse().join('')
  }
}

可以说使用 computed 性能会更好,但是如果你不希望缓存,你可以使用 methods 属性。

computed setter

http://kxdang.com/topic/vue2/vue-computed.html#computed-setter

computed 属性默认只有 getter ,不过在需要时你也可以提供一个 setter :

实例 4

http://kxdang.com/topic/vue2/vue-computed.html#%E5%AE%9E%E4%BE%8B-4

var vm = new Vue({
  el: '#app',
  data: {
    name: 'Google',
    url: 'http://www.google.com'
  },
  computed: {
    site: {
      // getter
      get: function () {
        return this.name + ' ' + this.url
      },
      // setter
      set: function (newValue) {
        var names = newValue.split(' ')
        this.name = names[0]
        this.url = names[names.length - 1]
      }
    }
  }
})
// 调用 setter, vm.name 和 vm.url 也会被对应更新
vm.site = '菜鸟教程 http://www.kxdang.com/topic/';
document.write('name: ' + vm.name);
document.write('<br>');
document.write('url: ' + vm.url);

从实例运行结果看在运行 vm.site = '菜鸟教程 http://www.kxdang.com/topic/'; 时,setter 会被调用, vm.name 和 vm.url 也会被对应更新。

Vue.js 自定义指令

http://kxdang.com/topic/vue2/vue-custom-directive.html#vuejs-

除了默认设置的核心指令( v-model 和 v-show ), Vue 也允许注册自定义指令。

下面我们注册一个全局指令 v-focus, 该指令的功能是在页面加载时,元素获得焦点:

实例

http://kxdang.com/topic/vue2/vue-custom-

<div id="app">
    <p>页面载入时,input 元素自动获取焦点:</p>
    <input v-focus>
</div>
<script>
// 注册一个全局自定义指令 v-focus
Vue.directive('focus', {
  // 当绑定元素插入到 DOM 中。
  inserted: function (el) {
    // 聚焦元素
    el.focus()
  }
})
// 创建根实例
new Vue({
  el: '#app'
})
</script>

我们也可以在实例使用 directives 选项来注册局部指令,这样指令只能在这个实例中使用:

实例

http://kxdang.com/topic/vue2/vue-custom-

<div id="app">
  <p>页面载入时,input 元素自动获取焦点:</p>
  <input v-focus>
</div>
<script>
// 创建根实例
new Vue({
  el: '#app',
  directives: {
    // 注册一个局部的自定义指令 v-focus
    focus: {
      // 指令的定义
      inserted: function (el) {
        // 聚焦元素
        el.focus()
      }
    }
  }
})
</script>

钩子

http://kxdang.com/topic/vue2/vue-custom-

钩子函数

http://kxdang.com/topic/vue2/vue-custom-

指令定义函数提供了几个钩子函数(可选):

  • bind: 只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作。
  • inserted: 被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于 document 中)。
  • update: 被绑定元素所在的模板更新时调用,而不论绑定值是否变化。通过比较更新前后的绑定值,可以忽略不必要的模板更新(详细的钩子函数参数见下)。
  • componentUpdated: 被绑定元素所在模板完成一次更新周期时调用。
  • unbind: 只调用一次, 指令与元素解绑时调用。

钩子函数参数

http://kxdang.com/topic/vue2/vue-custom-

钩子函数的参数有:

  • el: 指令所绑定的元素,可以用来直接操作 DOM 。
  • binding : 一个对象,包含以下属性: name : 指令名,不包括 v- 前缀。 value : 指令的绑定值, 例如: v-my-directive="1 + 1", value 的值是 2oldValue : 指令绑定的前一个值,仅在 updatecomponentUpdated 钩子中可用。无论值是否改变都可用。 expression : 绑定值的表达式或变量名。 例如 v-my-directive="1 + 1" , expression 的值是 "1 + 1"arg : 传给指令的参数。例如 v-my-directive:foo, arg 的值是 "foo"modifiers : 一个包含修饰符的对象。 例如: v-my-directive.foo.bar, 修饰符对象 modifiers 的值是 { foo: true, bar: true }
  • vnode: Vue 编译生成的虚拟节点。
  • oldVnode : 上一个虚拟节点,仅在 updatecomponentUpdated 钩子中可用。

以下实例演示了这些参数的使用:

实例

http://kxdang.com/topic/vue2/vue-custom-

<div id="app"  v-kxdang:hello.a.b="message">
</div>
<script>
Vue.directive('kxdang', {
  bind: function (el, binding, vnode) {
    var s = JSON.stringify
    el.innerHTML =
      'name: '       + s(binding.name) + '<br>' +
      'value: '      + s(binding.value) + '<br>' +
      'expression: ' + s(binding.expression) + '<br>' +
      'argument: '   + s(binding.arg) + '<br>' +
      'modifiers: '  + s(binding.modifiers) + '<br>' +
      'vnode keys: ' + Object.keys(vnode).join(', ')
  }
})
new Vue({
  el: '#app',
  data: {
    message: '菜鸟教程!'
  }
})
</script>

有时候我们不需要其他钩子函数,我们可以简写函数,如下格式:

Vue.directive('kxdang', function (el, binding) {
  // 设置指令的背景颜色
  el.style.backgroundColor = binding.value.color
})

指令函数可接受所有合法的 JavaScript 表达式,以下实例传入了 JavaScript 对象:

实例

http://kxdang.com/topic/vue2/vue-custom-

Vue.js 目录结构

http://kxdang.com/topic/vue2/vue-directory-structure.html#vuejs-

上一章节中我们使用了 npm 安装项目,我们在 IDE(Eclipse、Atom 等) 中打开该目录,结构如下所示:

目录解析

http://kxdang.com/topic/vue2/vue-directory-

在前面我们打开 APP.vue 文件,代码如下(解释在注释中):

src/APP.vue

http://kxdang.com/topic/vue2/vue-directory-structure.html#srcappvue

<!-- 展示模板 -->
<template>
  <div id="app">
    <img src="./assets/logo.png">
    <hello></hello>
  </div>
</template>
<script>
// 导入组件
import Hello from './components/Hello'
export default {
  name: 'app',
  components: {
    Hello
  }
}
</script>
<!-- 样式代码 -->
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

接下来我们可以尝试修改下初始化的项目,将 Hello.vue 修改为以下代码:

src/components/Hello.vue

http://kxdang.com/topic/vue2/vue-directory-

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>
<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: '欢迎来到菜鸟教程!'
    }
  }
}
</script>

重新打开页面 http://localhost:8080/,一般修改后会自动刷新,显示效果如下所示:

相关文章
|
4天前
|
JavaScript
vue消息订阅与发布
vue消息订阅与发布
|
22小时前
|
JavaScript
理解 Vue 的 setup 应用程序钩子
【10月更文挑战第3天】`setup` 函数是 Vue 3 中的新组件选项,在组件创建前调用,作为初始化逻辑的入口。它接收 `props` 和 `context` 两个参数,内部定义的变量和函数需通过 `return` 暴露给模板。`props` 包含父组件传入的属性,`context` 包含组件上下文信息。`setup` 可替代 `beforeCreate` 和 `created` 钩子,并提供类似 `data`、`computed` 和 `methods` 的功能,支持逻辑复用和 TypeScript 类型定义。
19 11
|
4天前
|
JavaScript 前端开发 IDE
Vue学习笔记5:用Vue的事件监听 实现数据更新的实时视图显示
Vue学习笔记5:用Vue的事件监听 实现数据更新的实时视图显示
|
4天前
|
JavaScript 前端开发 API
Vue学习笔记4:用reactive() 实现数据更新的实时视图显示
Vue学习笔记4:用reactive() 实现数据更新的实时视图显示
|
3天前
|
JavaScript
vue尚品汇商城项目-day07【vue插件-50.(了解)表单校验插件】
vue尚品汇商城项目-day07【vue插件-50.(了解)表单校验插件】
12 4
|
3天前
|
JavaScript
vue尚品汇商城项目-day07【51.路由懒加载】
vue尚品汇商城项目-day07【51.路由懒加载】
13 4
|
4天前
|
JavaScript 前端开发
Vue学习笔记8:解决Vue学习笔记7中用v-for指令渲染列表遇到两个问题
Vue学习笔记8:解决Vue学习笔记7中用v-for指令渲染列表遇到两个问题
|
3天前
|
JavaScript
vue尚品汇商城项目-day07【vue插件-54.(了解)生成二维码插件】
vue尚品汇商城项目-day07【vue插件-54.(了解)生成二维码插件】
9 2
|
4天前
|
JavaScript 前端开发 API
Vue学习笔记7:使用v-for指令渲染列表
Vue学习笔记7:使用v-for指令渲染列表
|
4天前
|
JavaScript
vue 函数化组件
vue 函数化组件
下一篇
无影云桌面