【转】vue技术分享-你可能不知道的7个秘密
一、善用watch的immediate属性
这一点我在项目中也是这么写的。例如有请求需要再也没初始化的时候就执行一次,然后监听他的变化,很多人这么写:
created(){
    this.fetchPostList()
},
watch: {
    searchInputValue(){
        this.fetchPostList()
    }
}上面的这种写法我们可以完全如下写:
watch: {
    searchInputValue:{
        handler: 'fetchPostList',
        immediate: true
    }
}二、组件注册,值得借鉴
一般情况下,我们组件如下写:
import BaseButton from './baseButton'
import BaseIcon from './baseIcon'
import BaseInput from './baseInput'
export default {
  components: {
    BaseButton,
    BaseIcon,
    BaseInput
  }
}
<BaseInput  v-model="searchText" @keydown.enter="search" />
<BaseButton @click="search">  <BaseIcon name="search"/></BaseButton>步骤一般有三部,
第一步,引入、
第二步注册、
第三步才是正式的使用,
这也是最常见和通用的写法。但是这种写法经典归经典,好多组件,要引入多次,注册多次,感觉很烦。
我们可以借助一下webpack,使用 require.context() 方法来创建自己的(模块)上下文,从而实现自动动态require组件。
思路是:在src文件夹下面main.js中,借助webpack动态将需要的基础组件统统打包进来。
代码如下:
import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
// Require in a base component context
const requireComponent = require.context(
  ‘./components’, false, /base-[\w-]+\.vue$/
)
requireComponent.keys().forEach(fileName => {
  // Get component config
  const componentConfig = requireComponent(fileName)
  // Get PascalCase name of component
  const componentName = upperFirst(
    camelCase(fileName.replace(/^\.\//, '').replace(/\.\w+$/, ''))
  )
  // Register component globally
  Vue.component(componentName, componentConfig.default || componentConfig)
})这样我们引入组件只需要第三步就可以了:
<BaseInput
  v-model="searchText"
  @keydown.enter="search"
/>
<BaseButton @click="search">
  <BaseIcon name="search"/>
</BaseButton>三、精简vuex的modules引入
对于vuex,我们输出store如下写:
import auth from './modules/auth'
import posts from './modules/posts'
import comments from './modules/comments'
// ...
export default new Vuex.Store({
  modules: {
    auth,
    posts,
    comments,
    // ...
  }
})要引入好多modules,然后再注册到Vuex.Store中~~
精简的做法和上面类似,也是运用 require.context()读取文件,代码如下:
import camelCase from 'lodash/camelCase'
const requireModule = require.context('.', false, /\.js$/)
const modules = {}
requireModule.keys().forEach(fileName => {
  // Don't register this file as a Vuex module
  if (fileName === './index.js') return
  const moduleName = camelCase(
    fileName.replace(/(\.\/|\.js)/g, '')
  )
  modules[moduleName] = {
                namespaced: true,
               ...requireModule(fileName),
              }
})
export default modules这样我们只需如下代码就可以了:
import modules from './modules'
export default new Vuex.Store({
  modules
})四、路由的延迟加载
这一点,关于vue的引入,我之前在vue项目重构技术要点和总结中也提及过,可以通过require方式或者import()方式动态加载组件。
{
  path: '/admin',
  name: 'admin-dashboard',
  component:require('@views/admin').default
}或者
{
  path: '/admin',
  name: 'admin-dashboard',
  component:() => import('@views/admin')
}加载路由。
五、router key组件刷新
下面这个场景真的是伤透了很多程序员的心...先默认大家用的是Vue-router来实现路由的控制。 假设我们在写一个博客网站,需求是从/post-haorooms/a,跳转到/post-haorooms/b。然后我们惊人的发现,页面跳转后数据竟然没更新?!原因是vue-router"智能地"发现这是同一个组件,然后它就决定要复用这个组件,所以你在created函数里写的方法压根就没执行。通常的解决方案是监听$route的变化来初始化数据,如下:
data() {
  return {
    loading: false,
    error: null,
    post: null
  }
},
watch: {
  '$route': {
    handler: 'resetData',
    immediate: true
  }
},
methods: {
  resetData() {
    this.loading = false
    this.error = null
    this.post = null
    this.getPost(this.$route.params.id)
  },
  getPost(id){
  }
}bug是解决了,可每次这么写也太不优雅了吧?秉持着能偷懒则偷懒的原则,我们希望代码这样写:
data() {
  return {
    loading: false,
    error: null,
    post: null
  }
},
created () {
  this.getPost(this.$route.params.id)
},
methods () {
  getPost(postId) {
    // ...
  }
}解决方案:给router-view添加一个唯一的key,这样即使是公用组件,只要url变化了,就一定会重新创建这个组件。
<router-view :key="$route.fullpath"></router-view>注:我个人的经验,这个一般应用在子路由里面,这样才可以不避免大量重绘,假设app.vue根目录添加这个属性,那么每次点击改变地址都会重绘,还是得不偿失的!
六、唯一组件根元素
场景如下:
(Emitted value instead of an instance of Error)
  Error compiling template:
  <div></div>
  <div></div>
  - Component template should contain exactly one root element.
    If you are using v-if on multiple elements, use v-else-if
    to chain them instead.模板中div只能有一个,不能如上面那么平行2个div。
例如如下代码:
<template>
  <li
    v-for="route in routes"
    :key="route.name"
  >
    <router-link :to="route">
      {{ route.title }}
    </router-link>
  </li>
</template>会报错!
我们可以用render函数来渲染
functional: true,
render(h, { props }) {
  return props.routes.map(route =>
    <li key={route.name}>
      <router-link to={route}>
        {route.title}
      </router-link>
    </li>
  )
}七、组件包装、事件属性穿透问题
当我们写组件的时候,通常我们都需要从父组件传递一系列的props到子组件,同时父组件监听子组件emit过来的一系列事件。举例子:
//父组件
<BaseInput
    :value="value"
    label="密码"
    placeholder="请填写密码"
    @input="handleInput"
    @focus="handleFocus>
</BaseInput>
//子组件
<template>
  <label>
    {{ label }}
    <input
      :value="value"
      :placeholder="placeholder"
      @focus=$emit('focus', $event)"
      @input="$emit('input', $event.target.value)"
    >
  </label>
</template>这样写很不精简,很多属性和事件都是手动定义的,我们可以如下写:
<input
    :value="value"
    v-bind="$attrs"
    v-on="listeners"
>
computed: {
  listeners() {
    return {
      ...this.$listeners,
      input: event =>
        this.$emit('input', event.target.value)
    }
  }
}$attrs包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外)。当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定,并且可以通过 v-bind="$attrs" 传入内部组件。
$listeners包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器。它可以通过 v-on="$listeners" 传入内部组件。
转自:http://bslxx.com/m/view.php?aid=1952
【转】vue技术分享-你可能不知道的7个秘密的更多相关文章
- vue技术分享-你可能不知道的7个秘密
		前言 本文是vue源码贡献值Chris Fritz在公共场合的一场分享,觉得分享里面有不少东西值得借鉴,虽然有些内容我在工作中也是这么做的,还是把大神的ppt在这里翻译一下,希望给朋友带来一些帮助. ... 
- vue技术分享之你可能不知道的7个秘密
		本文是vue源码贡献值Chris Fritz在公共场合的一场分享,觉得分享里面有不少东西值得借鉴,虽然有些内容我在工作中也是这么做的,还是把大神的ppt在这里翻译一下,希望给朋友带来一些帮助. 一.善 ... 
- 手机三种SIM卡 你所不知道的剪卡“秘密”
		SIM卡物理尺寸的发展是逐渐轻薄化,尺寸逐渐缩小的一个过程,最早手机中的卡都是2FF,2003年国际标准提出3FF,当前很多终端都使用这种形态的卡,4FF在2011年的国际标准会议中提出,2012年纳 ... 
- sublime text--你所不知道的12个秘密
		转自:http://blog.csdn.net/laokdidiao/article/details/51429404 package control安装: 第一种方法是在控制台中复制粘贴代码按回车, ... 
- 你可能不知道的github的秘密
		github也可以使用快捷键 先举例子,如何快速查找项目中的文件? 只需要进入项目,并按下T键 在浏览代码时,如何快速跳到指定行? 只需要进入项目,并按下L键 下面是一些常用的快捷键 聚焦搜索栏 按下 ... 
- SIM卡是什么意思?你所不知道的SIM卡知识扫盲(详解)【转】
		原文链接:http://www.jb51.net/shouji/359262.html 日常我们使用手机,SIM卡是手机的必须,没有了它就不能接入网络运营商进行通信服务.SIM卡作为网络运营商对于我们 ... 
- 你所不知道的html5与html中的那些事第三篇
		文章简介: 关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后,所有的工作 ... 
- 你所不知道的Html5那些事(一)
		文章简介: 关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后 ... 
- 你可能不知道的5种 CSS 和 JS 的交互方式
		翻译人员: 铁锚 翻译日期: 2014年01月22日 原文日期: 2014年01月20日 原文链接: 5 Ways that CSS and JavaScript Interact That You ... 
随机推荐
- mybatis(错误一) 项目启动时报“Result Maps collection already contains value forxxx”的解决方案
			Result Maps collection already contains value for xyx.dsw.dao.mapper.admin.quotationwish.TempTestTab ... 
- jquery.js 3.0报错, Uncaught TypeError: url.indexOf is not a function
			转载自:http://majing.io/questions/432 问题描述 jQuery升级到3.0.0后类型错误 jquery.js:9612 Uncaught TypeError: url ... 
- github Bash教程
			1.只在本地使用 配置github git config --global user.name 你的英文名 git config --global user.email 你的邮箱 git config ... 
- 力扣(LeetCode)226. 翻转二叉树
			翻转一棵二叉树. 示例: 思想 递归 java版 /** * Definition for a binary tree node. * public class TreeNode { * int va ... 
- Cordova+Vue快速搭建Hybrid App
			前言 最近项目迭代需要开发一个app,由于项目组其他系统前端技术栈都是Vue,所以自己在需求评估的时候就初步敲定了Cordova+Vue的前端架构,后来查阅了不少资料,也掉了不少坑,这里总结一下,也算 ... 
- 程序莫名其妙的崩溃,加断点刚进入函数就崩溃,断点不往下走,读取图片数据到程序并保存到一个HBITMAP 中
			HPR_INT32 CCarManageDlg::DrawPic2UIForm(IUIFormObj* pUIForm,string& strPicName){//程序崩溃的地方 HPR_IN ... 
- [Spring] 关联类和bean | autowire=byName|byType
			ref:https://www.tutorialspoint.com/spring/spring_autowiring_byname.htm project:Working Set: Spring&g ... 
- gradle ----> 安装和使用
			1.安装gradle 参考官网教程:https://gradle.org/install/ 安装的前提:要求安装jdk1.7或者以上 比较重要的一步:配置环境变量,把gradle的bin目录的全路径配 ... 
- 亚马逊商品页面的简单爬取 --Pyhon网络爬虫与信息获取
			1.亚马逊商品页面链接地址(本次要爬取的页面url) https://www.amazon.cn/dp/B07BSLQ65P/ 2.代码部分 import requestsurl = "ht ... 
- 20170907wdVBA_GetCellsContentToExcel
			'WORD 加载项 代码模板 Dim cmdBar As CommandBar, cmdBtn As CommandBarControl Const cmdBtnCap As String = &qu ... 
