找回密码
 立即注册
搜索
查看: 335|回复: 0

Vue应用与组件

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2024-12-26 10:48:18 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>vue</title>
  7.     <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  8. </head>
  9. <body>
  10.     <div id="Application">
  11.         <my-alert></my-alert>
  12.     </div>
  13.     <script>
  14.         const App = Vue.createApp({});  //使用Vue.createApp()创建Vue应用实例
  15.         const alertComponent = {
  16.             //data提供应用所需的全局数据
  17.             data() {
  18.                 return {
  19.                     msg: "警告框提示",
  20.                     count: 0
  21.                 }
  22.             },
  23.             //methods配置组件中需要用到的方法
  24.             methods: {
  25.                 click() {
  26.                     alert(this.msg + this.count++)
  27.                 },
  28.                 countChange(value, oldValue) {
  29.                     console.log(value, oldValue)
  30.                 }
  31.             },
  32.             //computed配置组件的计算属性
  33.             computed: {
  34.                 get() {
  35.                     return this.count + "次"
  36.                 }
  37.             },
  38.             //watch配置组件的监听函数
  39.             watch: {
  40.                 count: "countChange"
  41.             },
  42.             //tamplate配置组件的模板
  43.             template: `<div><button @click="click">按钮</button></div>`
  44.         }
  45.         //注册组件,应用实例可以用component()方法来定义组件,定义好组件后,组件就可以在模板中使用
  46.         App.component("my-alert", alertComponent);
  47.         //挂载应用,挂载到id为Application的元素上,创建好Vue应用实例后,使用mount()方法绑定到指定的HTML元素上
  48.         App.mount("#Application");
  49.     </script>
  50. </body>
  51. </html>
复制代码
  1. Vue.createApp(APP).mount("#Application")       //是一个汇总的简写
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 16:15 , Processed in 0.049321 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表