失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > vue项目中watch的immediate使用

vue项目中watch的immediate使用

时间:2020-10-01 12:54:03

相关推荐

vue项目中watch的immediate使用

web前端|js教程

vue,技术分享

web前端-js教程这一点我在项目中也是这么写的。例如有请求需要再也没初始化的时候就执行一次,然后监听他的变化,很多人这么写:

美化代刷网源码,ubuntu终端终止程序,tomcat启动慢知乎,爬虫福利27,php中assert,云龙区专业seo需要多少钱lzw

created(){ this.fetchPostList()},watch: { searchInputValue(){ this.fetchPostList() }}

上面的这种写法我们可以完全如下写:

delphi7源码学习,vscode多选替换,ubuntu如何烧录单片机程序,my安装tomcat,sqlite中文乱码问题,响应式日历选择插件,百度前端框架 开源,爬虫怎么下载文件,kingcms php,商品seo发布,磁力搜索网站 源码,静态网页设计免费下载,手机淘宝登陆网页模板下载lzw

watch: { searchInputValue:{ handler: fetchPostList, immediate: true }}

B、组件注册,值得借鉴

人人店v3.3.8 源码,vscode写vue配置,ubuntu 查询网关,更改tomcat默认网址,trip爬虫,值班系统 php,承德seo优化网络推广,免费企业网站程序上传,电脑模板在哪里下载 迅雷下载 迅雷下载lzw

一般情况下,我们组件如下写:

import BaseButton from ./baseButtonimport BaseIcon from ./baseIconimport BaseInput from ./baseInputexport default { components: { BaseButton, BaseIcon, BaseInput }}

步骤一般有三部,

第一步,引入、

第二步注册、

第三步才是正式的使用,

这也是最常见和通用的写法。但是这种写法经典归经典,好多组件,要引入多次,注册多次,感觉很烦。

我们可以借助一下webpack,使用require.context()方法来创建自己的(模块)上下文,从而实现自动动态require组件。

思路是:在src文件夹下面main.js中,借助webpack动态将需要的基础组件统统打包进来。

代码如下:

import Vue from vueimport upperFirst from lodash/upperFirstimport camelCase from lodash/camelCase// Require in a base component contextconst 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 ponent(componentName, componentConfig.default || componentConfig)})

这样我们引入组件只需要第三步就可以了:

C、精简vuex的modules引入

对于vuex,我们输出store如下写:

import auth from ./modules/authimport posts from ./modules/postsimport comments from ./modules/comments// ...export default new Vuex.Store({ modules: { auth, posts, comments, // ... }})

要引入好多modules,然后再注册到Vuex.Store中~~

精简的做法和上面类似,也是运用 require.context()读取文件,代码如下:

import camelCase from lodash/camelCaseconst requireModule = require.context(., false, /\.js$/)const modules = {}requireModule.keys().forEach(fileName => { // Don 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 ./modulesexport default new Vuex.Store({ modules})

D、路由的延迟加载

这一点,关于vue的引入,我之前在 vue项目重构技术要点和总结 中也提及过,可以通过require方式或者import()方式动态加载组件。

{ path: /admin, name: admin-dashboard, component:require(@views/admin).default}

或者

{ path: /admin, name: admin-dashboard, component:() => import(@views/admin)}

加载路由。

E、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: esetData, 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变化了,就一定会重新创建这个组件。

注:我个人的经验,这个一般应用在子路由里面,这样才可以不避免大量重绘,假设app.vue根目录添加这个属性,那么每次点击改变地址都会重绘,还是得不偿失的!

F、唯一组件根元素

场景如下:

(Emitted value instead of an instance of Error)

Error compiling template:

– 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.

模板中p只能有一个,不能如上面那么平行2个p。

例如如下代码:

{{ route.title }}

会报错!

我们可以用render函数来渲染

functional: true,render(h, { props }) { return props.routes.map(route =>

{route.title})}

G、组件包装、事件属性穿透问题

当我们写组件的时候,通常我们都需要从父组件传递一系列的props到子组件,同时父组件监听子组件emit过来的一系列事件。举例子:

//父组件//子组件

这样写很不精简,很多属性和事件都是手动定义的,我们可以如下写:

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"传入内部组件。

如果觉得《vue项目中watch的immediate使用》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。