当前位置: 首页 > article >正文

Vue3编写简单的App组件(二)

一、Vue3页面渲染基本流程

1、入口文件

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vite App</title>
</head>

<body>
  <div id="app"></div>
  <script type="module" src="/src/main.ts"></script>
</body>

</html>

2、main.ts

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

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

3、App.vue

一个Vue文件包含三部分:template(用来写html,搭建网页结构)、script(用来写js或ts,实现动态交互)和style(用来写css,实现网页样式)

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

</template>

<script lang="ts">
    // 写js或者ts
    export default {
        name: 'App' //组件名
    }

</script>

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

4、启动项目

二、实现一个复杂点的效果

1、Person.vue

<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:'13888888888'
      }
    },
    methods:{
      // 修改姓名
      changeName(){
        this.name = 'zhang-san'
      },
      // 修改年龄
      changeAge(){
        this.age += 1
      },
      // 展示联系方式
      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>

 2、App.vue

<template>
    <!-- 写html -->
    <h1>我是Maple:</h1>
    <div class="app">
        <h1>你好啊!</h1>
        <Person />
    </div>

</template>

<script lang="ts">

    import Person from './components/Person.vue'
    // 写js或者ts
    export default {
        name: 'App', //组件名
        components: { Person }
    }
</script>

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

3、main.js

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

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

4、index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vite App</title>
</head>

<body>
  <div id="app"></div>
  <script type="module" src="/src/main.ts"></script>
</body>

</html>

5、页面效果

>>点击修改姓名,姓名由Maple修改为Kelly:

>>点击修改年龄,年龄会加1:

>>点击查看电话,会弹窗显示电话号码:


http://www.kler.cn/news/232527.html

相关文章:

  • Linux系统安全之iptables防火墙
  • MATLAB实现LSTM时间序列预测
  • 7机器人位姿的数学描述与坐标变
  • windowsserver 2016 PostgreSQL9.6.3-2升级解决其安全漏洞问题
  • 后端的技术设计文档
  • 实例分割论文阅读之:FCN:《Fully Convolutional Networks for Semantica Segmentation》
  • impala与kudu进行集成
  • 【linux温故】linux调度机制
  • Ubuntu22.04 gnome-builder gnome C 应用程序习练笔记(二)
  • ArcGISPro中Python相关命令总结
  • 【RPA】智能自动化的未来:AI + RPA
  • 转融通业务是什么?好处和弊端是什么?
  • 全栈笔记_插件篇(用Volar替换Vuter)
  • Redis之基础篇
  • 【算法练习】leetcode算法题合集之其他篇
  • QT基础教程(全系列教程目录)
  • Java学习笔记------API
  • Flink Checkpoint过程
  • 一周学会Django5 Python Web开发-Django5创建项目(用命令方式)
  • Python数据分析 可视化数据Seaborn图表 这篇就够了
  • 【lesson47】进程通信之system V(共享内存)补充知识
  • PgSQL技术内幕 - case when表达式实现机制
  • 【Linux系统学习】3.Linux用户和权限
  • C++2024寒假J312实战班2.6
  • C语言冒泡排序介绍
  • 面试复盘——10
  • C++三剑客之std::any(一) : 使用
  • 【MySQL进阶之路】BufferPool底层设计(下)
  • 【GAMES101】Lecture 19 相机
  • java——学习并推荐java8