1.工程结构
vue2
// vue2引入的是一个构造函数, vue3引入的是一个createApp工厂函数
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
vue3
import Vue from 'vue'
import App from './App.vue'
new Vue({
render: h => h(App)
}).$mount('#app')
- setup()
<template>
<h1>setup</h1>
<hr>
<div>{{name}}</div>
<div>{{age}}</div>
<div>{{six}}</div>
<button @click="sayHello">vue3--sayHello</button> <br>
<button @click="sayHai">vue2--sayHai</button> <br>
<!-- vue2可以调vue3的方法 -->
<button @click="diaoSayHello">vue2调vue3</button> <br>
<!-- vue3调不了vue2的方法 vue3和vue2不建议一起使用-->
<button @click="diaoSayHai">vue3调vue2</button>
</template>
<script>
export default {
name: 'Vue3Study01Setup',
data() {
return {
six: '男',
}
},
methods:{
sayHai(){
alert('hai!')//能调
},
diaoSayHello(){
this.sayHello() //能调
}
},
setup(){
let name = 'lin';
let age = 18
function sayHello(){
alert('hello!')//能调
}
function diaoSayHai(){
console.log(this.sayHai());//报错
}
return {
name,age,sayHello,diaoSayHai
}
}
};
</script>
<style lang="scss" scoped>
</style>