简体   繁体   English

vue.js 中多个导入和注册的组件

[英]Multiple imported and registered components in vue.js

I am refactoring some code in my app and turns out,the below logic it is repeated in many many components.我正在重构我的应用程序中的一些代码,结果发现,下面的逻辑在许多组件中重复出现。

import component1 from '...'
import component2 from '...'
import component3 from '...'
//...many others

export default {
    //other data
  components: {
    component1,
    component2,
    component3
    //...
  }
}

Does exists a shorter approach in order to clean my code?是否存在更短的方法来清理我的代码? Thanks for your time谢谢你的时间

Below are 3 ways .I prefer method 3 by the way.以下是3种方法。顺便说一下,我更喜欢方法3。

Method 1方法一

Create a js file in my case dynamic_imports.js :在我的例子中创建一个js文件dynamic_imports.js

export default function (config) {
    let registered_components = {}
    for (let component of config.components) {
        registered_components[component.name] = () => System.import(`../${config.path}/${component.file_name}.vue`)
    }
    return registered_components
}

In the component in which you have many component imports and registrations在您有许多组件导入和注册的组件中

import dynamic_import from '@/services/dynamic_imports' //importing the above file
let components = dynamic_import({
    path: 'components/servers',
    components: [
        { name: 'server-one', file_name: 'serverOne' },
        { name: 'server-two', file_name: 'serverTwo' },
    ]
})

export default {
//...other code
    components: components
}

As a result you will import and register your components with "clean code".因此,您将使用“干净代码”导入和注册您的组件。 But note that this worked for me,maybe it has to modified a lit bit to fit your needs,to understand:请注意,这对我有用,也许它必须修改一点以满足您的需求,以了解:

The property path means that will look at this path for the names specified in file_name .The name property is the name you register the component属性path意味着将在此路径中查找file_name中指定的名称。 name属性是您注册组件的名称

Method 2 If you don't like the above look below to another way:方法2如果您不喜欢上面的方法,请看下面的另一种方法:

function import_component(cmp_name){
    return System.import(`@/components/${cmp_name}.vue`); 
}

export default{
    components: {
        'component1': () => import_component('componentOne'),
        'component2': () => import_component('componentTwo'),
        'component3': () => import_component('componentThree')
    }
}

Method 3 If again you are saying: This is not a cleaner way,take a look below but keep in mind that if you are working in team and skills differ,then some programmers will be a little bit confused.方法3如果你再次说:这不是一个更干净的方法,请看下面,但请记住,如果你是在团队中工作并且技能不同,那么一些程序员会有点困惑。

dynamic_imports.js动态导入.js

export default function ({path, file_names, component_names}) {
    let registered_components = {}
    for (let [index, file_name] of file_names.entries()) {
        registered_components[component_names[index]] = () => System.import(`../${path}/${file_name}.vue`)
    }
    return registered_components
}

In your component在您的组件中

import dynamic_import from '@/services/dynamic_imports'

let components = dynamic_import({
    path: 'components/servers',
    file_names: ['serverOne', 'serverTwo'],
    component_names: ['server-one', 'server-two']
})

export default {
    components: components
}

You can automatically register such repeated base components globally using the pattern described in the official docs您可以使用官方文档中描述的模式在全局范围内自动注册此类重复的基础组件

https://v2.vuejs.org/v2/guide/components-registration.html#Automatic-Global-Registration-of-Base-Components https://v2.vuejs.org/v2/guide/components-registration.html#Automatic-Global-Registration-of-Base-Components

Chris Fritz also talks about this pattern in his awesome video where he mentions 7 secret patterns for cleaner code and productivity boost while working with Vue.js Chris Fritz在他的精彩视频中也谈到了这种模式,他提到了 7 个秘密模式,用于在使用Vue.js时更简洁的代码和提高生产力

The disadvantage of this approach, however, is that the components that you autoregister this way always end up in the main bundle and therefore cannot be lazy loaded/code-splitted.然而,这种方法的缺点是,您以这种方式自动注册的组件总是在主包中结束,因此不能延迟加载/代码拆分。 So make sure you do this only for the base components that are very generic.因此,请确保仅对非常通用的基本组件执行此操作。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM