如何与nuxtjs一起使用vue-color
如何将Vue-Color导入我的项目?当我尝试导入和渲染组件时,
<template>
<div class="helloWorld">
<Chrome />
</div>
</template>
<script>
import { Chrome } from 'vue-color'
export default {
components: {
'chrome-picker': Chrome
},
}
</script>
我会收到此错误,
vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <Chrome> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
found in
---> <Anonymous>
<Root>
如何导入Vue-Color并将其在我的nuxt.js/vue.js项目中使用?
How do I import vue-color into my project? When I try to import and render the component like so
<template>
<div class="helloWorld">
<Chrome />
</div>
</template>
<script>
import { Chrome } from 'vue-color'
export default {
components: {
'chrome-picker': Chrome
},
}
</script>
I get this error
vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <Chrome> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
found in
---> <Anonymous>
<Root>
How do I import vue-color and use it in my nuxt.js/vue.js project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,Vue-Color不支持服务器端渲染,这是NUXT.JS遇到此组件问题的地方。您需要仅在客户端上渲染组件,这比将直接导入到文件中要多一些。
首先,您需要在插件目录中创建一个新文件,称为“ myplugin.client.js”。通过添加client.js零件,我们告诉nuxt.js,此插件仅在客户端上呈现。 (注意:还有一个.server.js扩展名仅用于服务器渲染)。
在此文件中,我们需要将我们的选择器添加到我们的项目中,并
保存以下文件,然后我们需要在nuxt.config.js中导入该文件。在该文件中,找到您的“插件”并添加文件。 在这种情况下,您的插件属性应该喜欢这样的东西
,“模式:'client'”有些多余,因为我们已经指定给NUXT,我们的插件仅在.client.js.js扩展很好地衡量。
现在,要引用我们的新组件,我们将在您的vue-color组件中添加以下代码,
现在应该按计划渲染。我希望这对我感到非常困惑,对此有所帮助。
As it turns out, vue-color does not support server-side rendering, which is where nuxt.js is running into problems with this component. You'll need to render the component only on the client-side, which is slightly more involved than importing directly to the file.
First, you'll want to create a new file in your plugins directory called "MyPlugin.client.js". By adding that client.js part, we are telling nuxt.js that this plugin is only to be rendered on the client. (Note: There is also a .server.js extension for server-only rendering).
In this file, we'll need to add our picker of choice to our project with the following
Save this file, then we're going to need to import that in our nuxt.config.js. In that file, find your "plugins" and add the file. Your plugins property should like something like this
In this case, "mode: 'client'" is slightly redundant, as we've already specified to nuxt that our plugin is client only with the .client.js extension - I just add it in for good measure.
Now, to reference our new component, we'll add the following code inside our html
Your vue-color component should now be rendering as planned. I hope this helps some people struggling with this, as I was very confused myself.
您的答案假设您将使用插件中的包装,这意味着:
25.5kb
如果要在一个地方加载
vue-color
,也可以使用一个地方,则可以使用在一个地方使用的解决方案我的在此处回答 aka aka这将确保仅在客户端和该页面/组件上本地加载软件包。
Your answer is supposing that you will use the package from a plugin, which means:
25.5kB
on top of the rest tooIf you want to load your
vue-color
in only one place, you could use the solution given at the end of my answer here akaThis will ensure that the package is loaded only on the client-side and locally on that page/component.