How to quickly get started with VUE3.0: The background to learning
this article came from an interview. I was asked, if vue and react components need to interoperate and reuse, how to implement it elegantly?
Except that developers manually transfer the code. Currently, I can think of only two solutions.
Solution 1: Convert vue code and react code (component library synchronization).
Solution 2: Directly let vue components run in the React project, and vice versa.
Let’s look at the implementation first
The vue component was rendered normally in react, and I also clicked the button 3 times . The response and render of vue were also normal .
The implementation principle is
to introduce the full version of vue (if you consider performance, you can introduce it on demand)
wait until the componentDidMount stage, mount <div id="vueApp" />
and then
new Vue(..).$mount('#vueApp')
import Vue from 'vue/dist/vue.min.js' //Introduce the complete version, otherwise vue's component object syntax cannot be parsed export default class App extends Component { constructor(props) { super(props) } componentDidMount() { const Foo = { template: ` <div> <h1>I am vue: {{aaa}}</h1> <h1>I am vue: {{aaa}}</h1> <h1>I am vue: {{aaa}}</h1> <button @click="aaa++">Button</button> </div> `, data () { return { aaa: 2222 } } } newVue({ render: h => h(Foo), }).$mount('#vueApp') } render() { return ( <div> <h1>Currently in the react project</h1> <h1>Currently in the react project</h1> The following will render the vue component <div id="vueApp" /> </div> ) } }
Note:
If you only need to support vue's component option object, then you don't need to configure webpack.
Vue's component option object refers to:
const Foo = { template: ` <div> <h1>I am vue: {{aaa}}</h1> <h1>I am vue: {{aaa}}</h1> <h1>I am vue: {{aaa}}</h1> <button @click="aaa++">Button</button> </div> `, data () { return { aaa: 2222 } } }
advanced version here refers to: need to support .vue files/components (the demo above is directly the component option object, there is no .vue file).
For example: (continue to use the demo above and change a few lines)
import Foo from "./Foo.vue";
import Vue from 'vue/dist/vue.min.js' //Introduce the full version, otherwise vue's component object syntax cannot be parsed import Foo from "./ Foo.vue"; export default class App extends Component { ... componentDidMount() { newVue({ render: h => h(Foo), }).$mount('#vueApp') } ... }
For this to take effect, you need to configure the vue-loader
// In webpack.config.js const { VueLoaderPlugin } = require('vue -loader') module.exports = { mode: 'development', module: { rules: [ { test: /.vue$/, loader: 'vue-loader' } ] }, plugins: [ // make sure to include the plugin for the magic new VueLoaderPlugin() ] }
Note:
It is recommended that when testing, do not use react scaffolding projects , but use react projects that configure webpack.config.js from scratch.
When I use the latest version of scaffolding, after running eject, go to webpack.config.js Writing in it will report an error because VueLoaderPlugin is not compatible with a oneOf syntax.
If you do not need to parse the .vue file and directly use vue's component option object syntax, then there is no need to configure additional vue-loader.
it is recommended that you do not use react when testing.
a scaffolded project , use a react project that configures webpack.config.js from scratch.
If you don’t need to parse the .vue file and directly use vue’s component option object syntax, then there is no need for additional configuration of vue-loader.
Finally, for comparison, the vue project React components are used in react projects, and vue components are used in react projects. The difference in configuration!
Do I need to configure the loader of webpack.config.js? | |
---|---|
When using the react component in a vue project | yes , you need to configure babel-loader and compile the .jsx file . You need to pay extra attention to the option of configuring babel-loader. |
using the vue component in a react project | no , if you do not need to parse the .vue file, use vue directly. If the component option object syntax is used, then there is no need to configure vue-loader additionally. If you need to support .vue files , you need to configure vue-loader. |
This article is reproduced from: https://juejin.cn/post/7083303446161391623
Author: bigtree