美文网首页
vue 语法风格指南

vue 语法风格指南

作者: 多年0以后 | 来源:发表于2023-05-11 13:42 被阅读0次

options API

// 基本写法
import { h } from 'vue';
export default {
  data() {
    return {
      msg: 'hello'
    };
  },
  render() {
    return h('div', this.msg);
  }
};

// 使用jsx
export default {
  data() {
    return {
      msg: 'hello'
    };
  },
  render() {
    return <div>{this.msg}</div>;
  }
};

// 使用ts
import { h, defineComponent } from 'vue';
type State = {
  msg: string;
};
export default defineComponent({
  data() {
    return {
      msg: 'hello'
    } as State;
  },
  render() {
    return h('div', this.msg);
  }
});

// 使用ts+jsx
import { defineComponent } from 'vue';
type State = {
  msg: string;
};
export default defineComponent({
  data() {
    return {
      msg: 'hello'
    } as State;
  },
  render() {
    return <div>{this.msg}</div>;
  }
});

Composition API

// 基本写法
import { h, reactive } from 'vue';
export default {
  setup() {
    const state = reactive({
      msg: 'hello'
    });
    return () => h('div', state.msg);
  }
};

// 使用jsx
import { reactive } from 'vue';
export default {
  setup() {
    const state = reactive({
      msg: 'hello'
    });
    return () => <div>{state.msg}</div>;
  }
};

// 使用ts
import { h, reactive, defineComponent } from 'vue';
type State = {
  msg: string;
};
export default defineComponent({
  setup() {
    const state = reactive({
      msg: 'hello'
    }) as State;
    return () => h('div', state.msg);
  }
});

// 使用ts+jsx
import { reactive, defineComponent } from 'vue';
type State = {
  msg: string;
};
export default defineComponent({
  setup() {
    const state = reactive({
      msg: 'hello'
    }) as State;
    return () => <div>{state.msg}</div>;
  }
});

在 SFC 中使用方法

  1. 在 script 标签中通过 lang 来定义使用的语言,如 lang="ts",lang="js",lang="jsx",lang="tsx"等
  2. 在使用 options API 时,使用方法与普通的 js/ts 文件一致
  3. 在使用 Composition API, 不使用 setup 标记的情况下,使用方法与普通的 js/ts 文件一致,

在使用 Composition API, 使用 setup 标记时,使用方法如下

使用 template 标签

<template>
  <div>{{ state.msg }}</div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
type State = {
  msg: string;
};
const state = reactive({
  msg: 'hello'
}) as State;
//
</script>
<style scoped lang="scss"></style>

使用 render 函数 + jsx

<script setup lang="ts">
import { reactive } from 'vue';
type State = {
  msg: string;
};
const state = reactive({
  msg: 'hello'
}) as State;
return () => <div>{state.msg}</div>;
</script>
<style scoped lang="scss"></style>

相关文章

  • Vue学习 Vue SSR + Vuex

    github 项目地址 预览 说明 vue学习整理 未严格按照Vue风格指南 旨在学习与交流vue语法以及基本入门...

  • Vue文档学习(第1遍)

    Vue教程: Vue API: Vue风格指南: Cookbook

  • 2018-08-02-MarkDown使用教程

    MarkDown使用教程 参考: Markdown 书写风格指南 Markdown 语法说明 语法 1.标题 标题...

  • VUE风格指南

    title: 风格指南type: style-guide 这里是官方的 Vue 特有代码的风格指南。如果在工程中使...

  • Vue 开发起步指南

    必须通读的文档 Vue 2.x 核心文档 Vuex 状态管理文档 Vue router 路由文档 Vue 风格指南...

  • vue学习第一课之vue的推荐风格指南

    vue推荐风格指南: 先把思维导图记下来了,后期再继续补充相关的吧 官方风格指南:https://cn.vuejs...

  • vue开发规范

    来自掘金的分享JS规范css规范vue风格指南官方文档

  • Vue风格指南

    风格指南官方文档 一、优先级A 1、组件名为多个单词 避免跟html元素相冲突。根组件除外。 2、组件data必须...

  • Vue风格指南(A)

    优先级A(必要的) 组件名 除了App外,其他组件名应为多单词组成的,因为HTML元素都是单单词的,这样可以避免冲...

  • VUE风格指南

    组件名为多个单词必要组件名应该始终是多个单词的,根组件 App 除外。 组件数据必要组件的 data必须是一个函数...

网友评论

      本文标题:vue 语法风格指南

      本文链接:https://www.haomeiwen.com/subject/gfyssdtx.html