惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
V
V2EX
MongoDB | Blog
MongoDB | Blog
量子位
J
Java Code Geeks
GbyAI
GbyAI
Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cloudbric
Cloudbric
Recorded Future
Recorded Future
月光博客
月光博客
Help Net Security
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
Scott Helme
Scott Helme
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
G
Google Developers Blog
T
Troy Hunt's Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
InfoQ
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
C
Check Point Blog
Project Zero
Project Zero
有赞技术团队
有赞技术团队
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
P
Privacy International News Feed
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Full Disclosure
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Hacker News: Front Page
S
Secure Thoughts
罗磊的独立博客
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园_首页
宝玉的分享
宝玉的分享
C
Cybersecurity and Infrastructure Security Agency CISA

博客园 - HorseShoe2016

Ethereum 学习笔记 ---- Solidity 数据位置(Calldata, Memory, Storage)编码与布局全解析 vscode 禁用警告提示音 浏览器加载 HTML 页面并执行 Javascript 的流程图 Ethereum学习笔记 ---- 多重继承中的 C3线性化算法 Ethereum学习笔记 ---- 使用 Remix 调试功能理解 bytes 在 memory 中的布局 Ethereum学习笔记 ---- 通过 Event 学习《合约ABI规范》 Javascript: Blob, File/FileReader, ArrayBuffer, ReadableStream, Response 转换方法 vscode 无法调试 golang testify suite 中的单个 test 的解决办法 纯js实现 vue 组件 与 vue 单文件组件对比 一个简单的 indexedDB 应用示例 html 元素的 onEvent 与 addEventListener vue3 动态编译组件失败:Component provided template option but runtime compilation is not supported in this build of Vue Javascript Object 中,isExtensible/isSealed/isFrozen 的对比 mini-vue: 响应式数据的实现 手写Promise vscode vue 插件与 emmet、tailwind css 插件冲突的解决方案 Python 安装依赖包,出现 ssl.SSLCertVerificationError 的问题,解决方法 CentOS7 源码编译安装 Python 3.8.10,开启 SSL 功能 Protocol Buffer Go (proto3) - macos 搭建 protocol buffer 环境 + Encoding 实验
vue3 defineComponent: 使用纯 Javascript 定义组件
HorseShoe2016 · 2024-03-24 · via 博客园 - HorseShoe2016

vuejs 官方文档参考:
definecomponent
渲染函数 API: h()

可以通过向 defineComponent() 传入一个 组合式 APIsetup function,或者 选项式 APIexport object,来定义一个组件,并包含各种响应式功能;如下 HomeAbout 组件所示:

<script setup>
import { ref, computed, defineComponent, h } from 'vue'

// 使用 `组合式 API` 的方式调用 defineComponent
const Home = defineComponent(
  (props) => {
    const colorIndex = ref(0)
    const colors = [
      // tailwindcss class
      'text-red-500',
      'text-green-500',
      'text-blue-500',
      'text-orange-500',
      'text-purple-500'
    ]
    function changeColor() {
      const oldIndex = colorIndex.value
      while (oldIndex == colorIndex.value) {
        colorIndex.value = Math.floor(Math.random() * colors.length)
      }
    }

    // 每次点击文字,会 call changeColor(), 从而导致 colorIndex 的 subscriber ---- render 的执行
    const render = () => {
      return h(
        'h1',
        { onClick: changeColor, class: colors[colorIndex.value] },
        `Hi ${props.name}, This is Home Page, click to change the text color`
      )
    }
    return render
  },
  {
    props: { name: { type: String, default: 'Alice' } }
  }
)

// 使用 `选项式 API` 的方式调用 defineComponent
const About = defineComponent({
  template: `<h1 @click="changeColor" :class="colors[colorIndex]">{{value}}</h1>`,
  data() {
    return {
      value: `Hello ${this.name}, This is About Page, click to change the text color`,
      colors: [
        // tailwindcss class
        'text-red-500',
        'text-green-500',
        'text-blue-500',
        'text-orange-500',
        'text-purple-500'
      ],
      colorIndex: 0
    }
  },
  props: {
    name: {
      type: String,
      default: 'Bob'
    }
  },
  methods: {
    changeColor() {
      const oldIndex = this.colorIndex
      while (oldIndex == this.colorIndex) {
        this.colorIndex = Math.floor(Math.random() * this.colors.length)
      }
    }
  },
  mounted() {
    // 由于 <keep-alive></keep-alive>的作用,使得页面切换时不销毁,所以 mounted() 只调用一次
    this.colorIndex = Math.floor(Math.random() * this.colors.length)
  }
})

const NotFound = defineComponent({
  template: `<h1>404</h1>`
})

const routes = {
  '/': Home,
  '/about': About
}

const currentPath = ref(window.location.hash)

window.addEventListener('hashchange', () => {
  currentPath.value = window.location.hash
})
const currentView = computed(() => {
  return routes[currentPath.value.slice(1) || '/'] || NotFound
})
</script>

<template>
  <div>
    <ul class="list-none flex justify-start space-x-5">
      <li><a href="#/">Home</a></li>
      <li><a href="#/about">About</a></li>
      <li><a href="#/non-existent-path">Broken Link</a></li>
    </ul>
    <keep-alive>
      <component :is="currentView" />
    </keep-alive>
  </div>
</template>

<style scoped>
li a {
  color: cornflowerblue;
}

h1 {
  font-size: 2em;
  font-weight: bold;
}
</style>

点击 Home / About 页面的文字,可以切换文字颜色:

posted on 2024-03-24 19:18  HorseShoe2016  阅读(3670)  评论()    收藏  举报