vue 实例和 vue 组件的区别? [英] Differences between vue instance and vue component?

查看:27
本文介绍了vue 实例和 vue 组件的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 vue js 的新手,在学习时有一些问题.

I'm new to vue js and have some questions when learning it.

我现在对它的实例和组件之间的关系有点困惑.据我了解,vue 构建的每个应用程序应该只有一个实例和一个实例,如果需要,它可以有任意数量的组件.但是最近我看到了一个demo,在那个demo里面有不止一个实例.

I'm now a little confused about the relationship between its instance and component. As far as I learned, every app build by vue should only have one instance and one instance only, it can have as many components as you like if needed. But recently I've seen a demo, and in that demo it has more than one instance.

所以我的问题是,在一个应用程序中有多个实例可以吗(演示代码工作正常,但这是正确的方法)?在 vue 应用中使用实例和组件的最佳实践是什么?

So my question is, is that ok to have multiple intances in one app( the demo code works fine, but is that the correct way)? What's the best practice to use instance and component in vue app?

推荐答案

在同一个项目中有两个实例是可以的,但是,您可能不想这样做.

It's ok to have two instances in the same project, however, you probably don't want to do that.

一个很好的方案是使用一个主实例来控制您的应用程序,特别是在您创建单页应用程序 (SPA) 时.然后根据需要使用尽可能多的组件.

A good scenario is to have one main instance to control your app, specially if you are creating a Single Page Application (SPA). Then use as many components as you want.

组件是重用代码并保持其井井有条的好方法,并且使用 Vue.js,可以非常轻松地在组件和主"Vue 实例之间进行通信.

Components are a great way to reuse code and keep it organized, and, with Vue.js, is very easy to communicate between your components and your "main" Vue instance.

这篇关于vue 实例和 vue 组件的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆