简体   繁体   English

Laravel如何导入通过NPM安装的vuejs软件包

[英]Laravel how do I import a vuejs package installed trough NPM

(Disclaimer: I'm not very proficient in Vue) (免责声明:我不太精通Vue)

What is the correct way to import/include a Vuejs package in my project? 在项目中导入/包含Vuejs包的正确方法是什么? I somehow seem to not be able to import a file. 我似乎无法导入文件。 I'm trying to import this package: https://github.com/sagalbot/vue-select 我正在尝试导入此软件包: https : //github.com/sagalbot/vue-select

What I have done so far: 到目前为止,我所做的是:

Ran the command: npm install vue-select 运行命令:npm install vue-select

Tried adding the vue-select package to my Vue like so (in resources/assets/js/app.js): 尝试将vue-select包添加到我的Vue中,如下所示(在resources / assets / js / app.js中):

import vSelect from './components/Select.vue'
Vue.component('v-select', vSelect);

Now when I use the v-select in my HTML file it gives this error in console: [Vue warn]: Unknown custom element: <v-select> - did you register the component correctly? For recursive components, make sure to provide the "name" option. 现在,当我在HTML文件中使用v-select时,会在控制台中显示以下错误: [Vue warn]: Unknown custom element: <v-select> - did you register the component correctly? For recursive components, make sure to provide the "name" option. [Vue warn]: Unknown custom element: <v-select> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

What I've tried to fix this: 我试图解决的问题:

Instead of using this in app.js I used it in my *.vue file, I don't use webpack/browserify I think, so import doesnt work :/ I prefer not to use browserify. 我没有在app.js中使用它,而是在我的* .vue文件中使用了它,我不使用webpack / browserify,所以导入不起作用:/我更不想使用browserify。

I have vue-resource package aswell and that created a vue-resource.js file in my public/js directory which I can import to use vue-resource. 我也有vue-resource包,它在我的public / js目录中创建了一个vue-resource.js文件,我可以导入该文件以使用vue-resource。 It seems like vue-select doesn't create its own vue-select.js file. 似乎vue-select不会创建自己的vue-select.js文件。

If you have any suggestions, please let me know. 如果您有任何建议,请让我知道。

I have looked at this question: Import vue package in laravel but I don't understand what paths and files the lines should be added to. 我已经看了这个问题: 在laravel中导入vue包,但我不知道应将行添加到哪些路径和文件中。

I think this is hust your import statement. 我认为这是您的import声明。 If you have installed it through npm, then you should import from vue-select . 如果已通过npm安装,则应从vue-select导入。 Also, use require instead of import , This way: 另外,使用require代替import ,这种方式:

Vue.component('v-select', require('vue-select'));

You may want to use it only in your Vue component, then you should load your own component as explained above, And in your own vue component say VueSelect.vue use this: 您可能只想在您的Vue组件中使用它,然后您应该按照上述说明加载自己的组件,并在您自己的vue组件中说VueSelect.vue使用此命令:

<template>
    <!-- use it with name 'v-select' in your component -->
    <v-select></v-select>
</template>
<script>
export default {
    ...
    components: { vSelect: require('vue-select') },
    ...
}
</script>

Then you can register your own component as global, 然后,您可以将自己的组件注册为全局组件,

Vue.component('vue-select', require( './components/VueSelect.vue') );

And use it in your root component: 并在您的根组件中使用它:

<vue-select></vue-select>

Update 更新

If you are using laravel, it gives you laravel mix out of the box which compiles down your assets using webpack. 如果您使用的是laravel,则可以立即使用laravel混音,使用webpack编译您的资产。 All you need to do is run a compile script: 您需要做的就是运行一个编译脚本:

npm run dev

If you are making changes to your js files, you wouldn't want to run this over and over, so you can run watch command: 如果您要更改js文件,则不想一遍又一遍地运行它,因此可以运行watch命令:

npm run watch

If this doesnt work for you, try watch-poll instead. 如果这对您不起作用,请尝试进行watch-poll Als, make sure you have installed all dependencies first: Als,请确保首先安装了所有依赖项:

npm install

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

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