iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue3中如何使用ts
  • 569
分享到

vue3中如何使用ts

2024-04-02 19:04:59 569人浏览 八月长安
摘要

目录如何使用tsapp.Vueheader.vuelist.vuelistitem.vuefooter.vue如何使用ts 在创建vue脚手架的时候吧typescript选上 app

如何使用ts

在创建vue脚手架的时候吧typescript选上

app.vue

<template>
  <!-- <div id="nav">
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link>
  </div>
  <router-view/> -->
  <div id="root">
    <div className="todo-container">
      <div className="todo-wrap">
        <Header :addtodo="addtodo"/>
        <List :todos="state.todos" />
        <Footer />
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, Reactive } from "vue";
import Footer from "./components/footer/footer.vue";
import Header from "./components/header/header.vue";
import List from "./components/list/list.vue";
//导入接口类型
import {todo} from "./type/todo"
export default defineComponent({
  components: { List, Header, Footer },
  setup() {
    const state = reactive({
      todos: [
        { id: 1, title: "吃饭", isture: true },
        { id: 2, title: "睡觉", isture: false },
        { id: 3, title: "打游戏", isture: false },
        { id: 4, title: "打代码", isture: true },
      ],
    });
    //定义一个方法用来接收输入框里面传来的数据并把它加到state.todos里面面
    const addtodo=(todo:todo)=>{
      state.todos.unshift(todo)
    }
    return {
      state,
      addtodo
    };
  },
});
</script>
<style>
@import "./App.CSS";
</style>

header.vue

<template>
  <div class="todo-header">
    <input
      type="text"
      placeholder="请输入你的任务名称,按回车键确认"
      v-model="title"
      @keyup.enter="add"
    />
  </div>
</template>
<style scoped>
@import "./header.css";
</style>
<script lang="ts">
import { defineComponent, ref } from "vue";
//导入接口类型
import { todo } from "../../type/todo";
export default defineComponent({
  props: {
    addtodo: {
      type: Function,
      required: true,
    },
  },
  setup(props) {
    const title = ref("");
    const add = () => {
      if (title.value == "") {
        alert("请输入内容");
      }
      const todo: todo = {
        id: Date.now(),
        title: title.value,
        isture: false,
      };
      props.addtodo(todo);
      title.value=''
    };
    return {
      add,
      title,
    };
  },
});
</script>

list.vue

<template>
    <ul className="todo-main">
         <Listitem v-for="(todos,index) in todos" :key="todos.id" :todos='todos'></Listitem>
    </ul>
</template>
<style scoped>
@import"./list.css";
</style>
<script lang="ts">
import Listitem from "./listitem/listitem.vue"
import { defineComponent } from 'vue'
export default defineComponent({
    components:{
        Listitem
    },
    props:{
        todos:Object
    }
    ,
    setup() {
        
    },
})
</script>

listitem.vue

<template>
 
    <li  class="itli" >
      <label>
        <input type="checkbox" v-model="todos.isture" />
        <span>{{todos.title}}</span>
      </label>
      <button
        class="btn btn-danger"
             >删除</button>
    </li>
 
</template>
<style scoped>
@import "./listitem.css";
</style>
<script lang="ts">
import { defineComponent } from "vue";
//导入接口类型
import {todo} from "../../../type/todo"
export default defineComponent({
  setup() {},
  props:{
//定义todos的类型是自己定义的todo接口
    todos:Object as ()=>todo
  }
});
</script>

footer.vue

<template>
    <div class="todo-footer">
        <label>
          <input type="checkbox"     />
        </label>
        <span  >
          <span >已完 </span> / 全部 
        </span>
        <button class="btn btn-danger"  >清除已完成任务</button>
      </div>
</template>
<style scoped>

</style>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
    setup() {
        
    },
})
</script>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: vue3中如何使用ts

本文链接: https://www.lsjlt.com/news/150127.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作