npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-vue-setup

v0.1.0

Published

You can use sugar in html in a format similar to vue3 setup syntactic sugar

Downloads

63

Readme

html-vue-setup

什么是html-vue-setup

html-vue-setup 是一个用于在 HTML 中使用 Vue 3 的工具。它允许你在 HTML 文件中直接使用类似 Vue 3 setup 语法糖的语法

快速上手

  1. 必须先在 HTML 文件中引入 Vue3 的 CDN 链接
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  1. 在 HTML 文件中引入 html-vue-setup
<script src="https://unpkg.com/html-vue-setup/dist/main.umd.js"></script>
  1. 在 HTML 文件中使用 setup 语法糖
<script setup>
  const count = ref(0)
</script>

基本特性

1.自动挂载

  1. 不使用 html-vue-setup 时,我们需要手动调用 createApp 函数,并传入一个对象,然后在对象中定义 setup 函数,并返回需要暴露的数据
<script>
  const { createApp } = Vue;
  createApp({
    setup: () => {
      /* 您的代码逻辑 */
      return {
        /* 需要暴露的数据 */
      };
    }
  }).mount('#app');
</script>
  1. 使用 html-vue-setup 后,爽
<script setup>
  /* 您的代码逻辑 */
</script>

挂载位置

  1. 默认挂载

默认挂载到#app上 (id为app的元素) 若没有则挂载到body下的第一个元素上

所以你可以这样写

<script setup>
  const count = ref(0)
</script>
<main>
  <div>{{ count }}</div>
</main>

浏览器会将<main>标签挂载到<body>下 然后html-vue-setup会把vue实例挂载到<main>

这种写法是为了更接近vue的SFC格式,也是本人的习惯写法,可能存在其他潜在问题

  1. 自定义挂载
<script setup="#my-app">
  const count = ref(0)
</script>

setup属性值最终会传参到mount函数中

暴露数据

setup函数中需要手动return需要暴露的数据,这个十分影响开发体验,所以html-vue-setup会自动将setup函数中定义的变量,自动return出去,不需要手动return

暂不支持命名解构

<script setup>
  const count = ref(0)
</script>
<main>
  <div>{{ count }}</div>
</main>

2.自动导入

  1. 不使用 html-vue-setup 时,我们需要手动解构 Vue 3 的函数,或者都带上 Vue 前缀
<script>
const { ref, createApp } = Vue;
  createApp({
    setup: () => {
      const count = ref(0);
      return {
        count,
      };
    }
  }).mount('#app');
</script>
  1. 使用 html-vue-setup 后,我们可以直接使用,不需要手动解构,或者都带上 Vue 前缀
<script setup>
  const count = ref(0)
</script>

书写顺序

vue3三更建议 <script setup>写在<template>之前 所以html-vue-setup也支持了这种写法

<!-- 可以写在视图前面 -->
<script setup>
  const count = ref(0)
</script>
<main>
  <div>{{ count }}</div>
</main>

更多示例

示例