Vue3 项目结构

发布于:2024-08-24 ⋅ 阅读:(136) ⋅ 点赞:(0)

1.main.ts

2.简单写一个src下的结构

App.vue 根组件 

<template>
  <div class="app">
    <!-- html -->
     <h1>你好啊!</h1>
  </div>
</template>

<script lang="ts">
//js 或 ts

export default {
    name:'App',//组件名
};


</script>

<style scoped>
/* css */
.app{
    background-color: #ddd;
    box-shadow: 0 0 10px;
    border-radius: 10px;
    padding: 20px;
}
</style>

 main.ts

//引入 createApp用于创建应用
import {createApp} from 'vue';
//引入 App 根组件
import App from './App.vue';

createApp(App).mount('#app');

3.回顾vue2

<template>
  <div class="person">
    <h2>姓名:{{ name }}</h2>
    <h2>年龄:{{ age }}</h2>

    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="showTel">查看联系方式</button>
  </div>
</template>

<script lang="ts">
export default {
  name: "Person",
  data() {
    return {
      name: "张三",
      age: 18,
      tel: "123456",
    };
  },
  methods: {
    changeAge() {
      this.age = 13;
    },
    changeName() {
      this.name = "zhang-san";
    },
    showTel() {
      alert(this.tel);
    },
  },
};
</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
button{
    margin:  0 5px;
}
</style>

证明Vue3 能向下兼容 vue2 的语法


网站公告

今日签到

点亮在社区的每一天
去签到