简体   繁体   English

vue.js / vue-router-使用道具创建单个文件组件的实例以路由到

[英]vue.js / vue-router - create instance of single file component with props to route to

My goal is to further reduce code and the most obvious one in my project is to abstract lists. 我的目标是进一步减少代码,而在我的项目中最明显的是抽象列表。 Currently I have a vue single file component called List which includes an async data loader. 目前,我有一个名为List的vue单个文件组件,其中包括一个异步数据加载器。 I've also created a bunch of 'derived' single file components using this List component as root tag element, passing props as needed to load the correct data. 我还使用此List组件作为根标签元素,创建了一堆“派生”单个文件组件,并根据需要传递道具以加载正确的数据。

Now, since I've split up my components into separate files using this plugin it is common to have a folder structure which looks like this: 现在,由于使用插件将组件拆分成单独的文件,因此通常具有如下所示的文件夹结构:

\components\
\components\List\
\components\List\List.vue
\components\List\List.vue.js
\components\List\List.scoped.vue.css
\components\List\List.vue.html

As you can see, 4 files per component. 如您所见,每个组件4个文件。 Imagine having 10 different list components all using List as their base. 想象一下,有10个不同的列表组件都以List为基础。 That is 10 folders with a total of 40 files. 那是10个文件夹,总共40个文件。 And for what? 为了什么 Pretty much the same code, 2-3 values that change (the properties), the rest stays the same. 几乎相同的代码,有2-3个变化的值(属性),其余的保持不变。

I've been trying to adjust List so that I can create an instance of it and pass the properties as constructor values. 我一直在尝试调整List以便可以创建它的实例并将属性作为构造函数值传递。 Why? 为什么? Instead of having 1 folder with 4 files per list, I could just have the base List and the create the components like so: 我可以只拥有基本的List并像下面这样创建组件,而不是拥有每个列表4个文件的1个文件夹:

let FooList = new List('foo', true, {}, (x) => {});
let BarList = new List('bar', false, {}, (y) => {});

I want to use these objects in the vue-router like so: 我想像这样在vue-router中使用这些对象:

const router = new Router({
  ...
  routes: [
    {
      path: "some/foo,
      component: FooList,
    },{
      path: "any/bar,
      component: BarList,
    },
  ]
});

Anything I tried failed. 我尝试的所有操作均失败。 What have I tried so far? 到目前为止,我尝试了什么?

export default { ... } exports a default single file component. export default { ... }导出默认的单个文件组件。 I figured if this is a component, I might as well just override some values in it. 我发现这是否是一个组件,我不妨覆盖其中的一些值。

How did I try to do this? 我如何尝试做到这一点?

I tried using Object.assign({ ... }, List) in the hope of creating a List object which has the properties defined like I want them to be. 我尝试使用Object.assign({ ... }, List) ,希望创建一个List对象,该对象的定义的属性与我想要的一样。 I also tried using the vue built in "extends" option of single files components to extend List , but this doesn't save code at all since I still need to define a template/render method for the component .. which results in those 4 files again. 我还尝试使用内置的单个文件组件的“ extends”选项的vue来扩展List ,但这根本不保存代码,因为我仍然需要为该组件定义模板/渲染方法。这导致这4个文件再次。 I tried to use Vue.component(..) and Vue.extend(..) , alone and in combination but couldn't succeed. 我尝试单独使用Vue.component(..)Vue.extend(..)并结合使用,但无法成功。

Anything I tried resulted either in a max stack exceeded exception (recursion gone wrong), vue errors were thrown stating that something doesn't fit or just not displaying anything at all. 我尝试的任何结果均导致最大堆栈超出异常(递归出错),抛出vue错误,表明某些内容不合适或根本不显示任何内容。

Is there a way of doing this? 有办法吗?

You could define a prop on the List component to specify the type of list and modify the behavior. 您可以在List组件上定义prop以指定列表的类型并修改行为。

let routes = [
  {
    path: '/home',
    name: 'home',
    component: List,
    props: { config: { type: 'Listing', display: 'Tile' } }
  },
]

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

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