简体   繁体   English

如何在Laravel中的app.js中正确添加多个Vue组件

[英]How to properly add multiple Vue components in app.js in Laravel

So, hi. I've been struggling for 4 hours to add at least two different Vue components to my app.js file which is located in js folder along with these vue components in my Laravel project. 我一直在努力4个小时,以将至少两个不同的Vue组件与Laravel项目中的这些vue组件一起添加到位于js文件夹中的app.js文件中。

I couldn't find any real solution on the internet since it didn't fix my problem. 我无法在互联网上找到任何真正的解决方案,因为它不能解决我的问题。

I have tried something like 我已经尝试过类似的东西

Vue.component('signature-element', require('./components/Signature.vue').default);

under the other component which works perfectly, but the problem is I can't make the app work with 2 or more components. 在另一个工作正常的组件下,但问题是我无法使该应用程序使用2个或更多组件。

I've tried installing vue-router via npm and configuring a router but it didn't work too, somehow whole JS stopped without any errors in the command log in browser or in Mix. 我试过通过npm安装vue-router并配置路由器,但是它也没有用,以某种方式,整个JS停止了,浏览器或Mix中的命令日志中没有任何错误。

I've also tried calling the import function instead of the first one I've mentioned, for example: 我还尝试过调用import函数,而不是我提到的第一个函数,例如:

const componentVar = import ...;

inside vue:

    new Vue({
        components: { first, componentVar },
        mounted() {}
    });

But this also did not work unfortunately. 但这也不幸地没有起作用。

In the second example you're trying to registrate locally your components. 在第二个示例中,您尝试在本地注册组件。

If you want to do this you can do it like this: 如果您想这样做,可以这样:

import first from './components/first'
import componentVar from './components/componentVar'

new Vue({
  el: '#app',
  components: {
    'first': first,
    'componentVar': componentVar
  }
})

Instead if you want to use the first example this means you want to register your components globally. 相反,如果要使用第一个示例,则意味着要全局注册组件。 That means they can be used in the template of any root Vue instance (new Vue) created after registration 这意味着它们可以在注册后创建的任何根Vue实例(新Vue)的模板中使用

Example: 例:

Vue.component('signature-element', { /* ... */ })

new Vue({ el: '#app' })

Then in your view 然后在您看来

<div id="app">
  <component-a></component-a>
  <component-b></component-b>
  <component-c></component-c>
</div>

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

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