繁体   English   中英

我不能在 Nuxt.js/vue.js 中使用第三方组件

[英]I can't use third party components in Nuxt.js/vue.js

我尝试将此库用于我的 Nuxt 项目:入门

我试过如何在文档中编写,但在所有变体中都会出现错误,例如:未知的自定义元素:-您是否正确注册了组件?

对于递归组件,请确保提供“名称”选项。 我试过的:

<template>
    <div class="div-wrapper">
        <h1>grge</h1>
        <div id="typeahead"><typeahead :data="USstate" placeholder="USA states">
        </typeahead></div>



    </div>
</template>
<style lang="less">
    .div-wrapper {
        background: #f4f4f4;
        padding: 95px 15px 50px 15px;
    }
</style>
<script>
    import Vue from 'vue';
    export default {
        data() {
            return {
                USstate: ['Alabama', 'Alaska', 'Arizona'],
                asyncTemplate: '{{ item.formatted_address }}',
                githubTemplate: '<img width="18px" height="18px" :src="item.avatar_url"> <span>{{item.login}}</span>'
            }
        },
        mounted(){
            var typeahead = require('vue-strap/src/Typeahead');
            Vue.component('typeahead',typeahead);
            new Vue({
                el: 'typeahead'
            })
        },
        methods: {
            googleCallback(items, targetVM) {
                const that = targetVM;
                that.reset()
                that.value = items.formatted_address
            },
            githubCallback(items) {
                window.open(items.html_url, '_blank')
            }
        }
    }
</script>

得到错误:窗口未定义。 比我试试这个:

mounted(){
        var typeahead = require('vue-strap/src/Typeahead');
        Vue.component('typeahead',typeahead);
        new Vue({
            el: 'typeahead'
        })
    }

渲染但有很多错误:错误图像

并尝试将如何在 ru.nuxtjs.org/examples/plugins 中描述为插件但未成功。 请帮我正确插入这个库。

我在 vue-touch 上遇到了同样的问题,我按照 Nuxtjs.Org 的建议将它添加为插件来解决它

工作流程

  • 在主目录“plugins”上创建一个文件夹
  • 在“plugins/the-external-component-you-want-to-add.js”下创建一个 .js 文件,用于可能的情况“plugins/vue-notifications.js”
  • 根据您的需要修改下面的代码。

     import Vue from 'vue' import VueTouch from 'vue-touch'

    客户端和服务器端使用

    Vue.use(VueTouch, {name: 'v-touch'})

    如果插件只需要客户端

    if (process.BROWSER_BUILD) { Vue.use(VueTouch, {name: 'v-touch'}) }

    然后是一个不错的日志

    console.log('plugin v-touch is locked and loaded')
  • 通过编辑 nuxt.config.js 将您的插件注入 webpack 工作流程

    plugins: ['~plugins/vue-touch'], build: { ... }
  • 然后您可以按照您在插件文件中的名称使用它。

     <v-touch @swipe="onswipeleft" class="dragme">SWIIIIIIPE</v-touch>

文档

有关更多信息,您可以查看 nuxtjs.org文档

暂无
暂无

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

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