欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

typescript在vue中的入門(mén)案例代碼demo

 更新時(shí)間:2022年12月30日 17:21:12   作者:微雨即至  
這篇文章主要介紹了typescript在vue中的入門(mén)案例代碼demo,使用技術(shù)棧vue2+typescript+scss入門(mén)練手項(xiàng)目,天氣預(yù)報(bào)demo,需要的朋友可以參考下。

使用技術(shù)棧vue2+typescript+scss入門(mén)練手項(xiàng)目,天氣預(yù)報(bào)demo,需要的朋友可以參考下。整體的實(shí)現(xiàn)思路比較簡(jiǎn)單,頁(yè)面也只有一個(gè),包含三部分,搜索框、熱門(mén)城市和天氣預(yù)報(bào),組件庫(kù)用的是ElementUI。

搜索框searchBox.vue

<template>
  <div id="search">
    <el-input
      placeholder="請(qǐng)輸入內(nèi)容"
      suffix-icon="el-icon-search"
      v-model="city"
      @change="enter"
      @input="edit"
    >
    </el-input>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Emit } from "vue-property-decorator";

@Component({
  components: {},
})
export default class searchBox extends Vue {
  city = "";

  @Emit("sendCity")
  enter(value: string): string {
    //在輸入框失去焦點(diǎn)或用戶(hù)按下回車(chē)時(shí)觸發(fā)(會(huì)導(dǎo)致一些bug?。。。?
    console.log("按下回車(chē),搜索地是:" + value);
    return value;
  }

  edit(value: string | number): void {
    console.log("正在輸入中......" + value);
  }
}
</script>

這里不寫(xiě)@component注解,會(huì)導(dǎo)致組件中的數(shù)據(jù)和處理方法不是響應(yīng)式的,剛開(kāi)始寫(xiě)的時(shí)候根本沒(méi)有意識(shí)到這個(gè)問(wèn)題,點(diǎn)擊頁(yè)面中的輸入框el-input組件根本沒(méi)反應(yīng),一直覺(jué)得是邏輯或者element有問(wèn)題,后來(lái)查了資料才知道是@component沒(méi)有寫(xiě)。searchBox子組件需要把自己的數(shù)據(jù)傳給weather組件,兩個(gè)組件為兄弟關(guān)系,通信的話(huà)可以借助父組件Home組件。在ts中,組件間的數(shù)據(jù)傳遞,通過(guò)@Emit,子組件將數(shù)據(jù)發(fā)送出去,父組件通過(guò)函數(shù)接收,而父組件與子組件通信,通過(guò)@Prop。

首頁(yè)Home.vue

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png" />
    <searchBox @sendCity="sendCity" />
    <popularCity @popularCity="clickCity" />
    <weather :searchCity="city" :popularCity="city" />
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import searchBox from "@/components/searchBox.vue";
import popularCity from "@/components/popularCity.vue";
import weather from "@/components/weather.vue";

@Component({
  components: {
    searchBox,
    popularCity,
    weather,
  },
})
export default class Home extends Vue {
  city = "";

  sendCity(city: string): void {
    //搜索框組件向home組件傳值
    this.city = city;
  }

  clickCity(city: string): void {
    //熱門(mén)城市傳值
    this.city = city;
  }
}
</script>

熱門(mén)城市 popularCity.vue

<template>
  <div id="city">
    <div v-for="(item, index) in message" :key="index">
      <el-button class="box-city" type="text" @click="clickCity(item)">{{
        item
      }}</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Emit } from "vue-property-decorator";

@Component({
  components: {},
})
export default class searchBox extends Vue {
  message = ["北京", "上海", "深圳", "成都", "重慶", "武漢", "南京"];

  @Emit("popularCity")
  clickCity(city: string): string {
    console.log("點(diǎn)擊熱門(mén)城市:" + city);
    return city;
  }
}
</script>

<style lang="scss" scoped>
@import "../style/index.scss";
#city {
  width: 40%;
  @include box-row-flex(center);

  .box-city {
    width: 10%;
    font-style: italic;
    color: $text-color;
    font-size: $font-size;
  }
}
</style>

這個(gè)沒(méi)有什么好說(shuō)的,主要就是進(jìn)行了scss的一些嘗試,比如@mixin

天氣 weather.vue

<template>
  <div id="weather">
    <div v-for="(item, index) in weatherArr" :key="index">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>{{ city }}</span>
        </div>
        <div class="content">
          <div class="type">{{ item.type }}</div>
          <div class="temp">
            {{ item.low | temperatureFilter }} ~
            {{ item.high | temperatureFilter }}
          </div>
          <div class="time">{{ item.date }}</div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts">
import weather from "../interface/IWeather";
import getWeather from "../utils/getWeather";
import { Vue, Component, Prop, Watch } from "vue-property-decorator";

@Component({
  components: {},
  filters: {
    //過(guò)濾器
    temperatureFilter: (value: string): string => {
      return value.substring(2);
    },
  },
})
export default class searchBox extends Vue {
  @Prop({
    type: String,
    default: "",
  })
  searchCity!: string;

  city = "西安";
  weatherArr: Array<weather> = [];

  @Watch("searchCity")
  async handleWatch(value: string): Promise<void> {
    console.log("搜索框或熱門(mén)城市傳入的地區(qū)是:" + value);

    const res = await getWeather(value);
    console.log(res);
    if (res.status == 1000) {
      this.city = value;
      this.weatherArr.length = 0; //清空當(dāng)前數(shù)組存入的數(shù)據(jù)
      this.weatherArr.push(...res.weather);
    } else if (res.status == 1002) {
      this.$message({
        message: res.desc as string,
        type: "error",
      });
    }
  }

  async created(): Promise<void> {
    const res = await getWeather("西安");
    if (res.status == 1000) {
      this.weatherArr.push(...res.weather);
    } else if (res.status == 1002) {
      this.$message({
        message: res.desc as string,
        type: "error",
      });
    }

    console.log(res);
  }
}
</script>

這里是整個(gè)demo的核心,負(fù)責(zé)接收其他組件的數(shù)據(jù),發(fā)送請(qǐng)求,獲取天氣數(shù)據(jù)。

先來(lái)說(shuō)一下其他組件傳遞數(shù)據(jù),上面說(shuō)了父子組件通信通過(guò)@Prop,這里用了@Watch檢測(cè)數(shù)據(jù)變化,如果點(diǎn)擊了某個(gè)熱門(mén)城市或者搜索框按下回車(chē)鍵,會(huì)發(fā)送數(shù)據(jù)到這部分,數(shù)據(jù)來(lái)了就通過(guò)axios發(fā)送請(qǐng)求,獲取天氣數(shù)據(jù)。這里關(guān)于發(fā)送網(wǎng)絡(luò)請(qǐng)求的部分,進(jìn)行了封裝。

同時(shí),根據(jù)接口的返回?cái)?shù)據(jù)寫(xiě)interface,這里為了展示數(shù)據(jù)(同時(shí)為了根據(jù)不同的返回碼status來(lái)提示不同的消息),創(chuàng)建接口IWeather,主要用來(lái)抽象天氣數(shù)據(jù),IFiveWeather用來(lái)抽象接口返回形式(接口的代碼就不在此展示)

getWeather.ts

import axios from "axios";

//獲取某地的天氣
async function getWeather(city: string): Promise<IFiveWeather> {
  const weatherArr: IFiveWeather = {
    status: 0,
    weather: [],
  };
  try {
    const res = await axios.get(
      `http://wthrcdn.etouch.cn/weather_mini?city=${city}`
    );

    const status: number = res.data.status;

    switch (status) {
      //輸入城市錯(cuò)誤的返回碼
      case 1002:
        weatherArr.status = 1002;
        weatherArr.desc = res.data.desc;
        weatherArr.weather = [];
        break;
      //數(shù)據(jù)返回成功
      case 1000:
        weatherArr.status = 1000;
        weatherArr.weather.push(...res.data.data.forecast);
    }
  } catch (error) {
    console.log("天氣接口出錯(cuò)啦:" + error);
  }

  return weatherArr;
}

export default getWeather;

到此這篇關(guān)于typescript在vue中的入門(mén)案例代碼demo的文章就介紹到這了,更多相關(guān)typescript入門(mén)demo內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • TypeScript數(shù)據(jù)結(jié)構(gòu)棧結(jié)構(gòu)Stack教程示例

    TypeScript數(shù)據(jù)結(jié)構(gòu)棧結(jié)構(gòu)Stack教程示例

    這篇文章主要為大家介紹了TypeScript數(shù)據(jù)結(jié)構(gòu)棧結(jié)構(gòu)Stack教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • TypeScript中的聯(lián)合類(lèi)型使用示例詳解

    TypeScript中的聯(lián)合類(lèi)型使用示例詳解

    這篇文章主要為大家介紹了TypeScript中的聯(lián)合類(lèi)型使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • TypeScript 交叉類(lèi)型使用方法示例總結(jié)

    TypeScript 交叉類(lèi)型使用方法示例總結(jié)

    這篇文章主要為大家介紹了TypeScript 交叉類(lèi)型使用方法示例總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • TypeScript實(shí)現(xiàn)十大排序算法之冒泡排序示例詳解

    TypeScript實(shí)現(xiàn)十大排序算法之冒泡排序示例詳解

    這篇文章主要為大家介紹了TypeScript實(shí)現(xiàn)十大排序算法之冒泡排序示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • typescript難學(xué)嗎?前端有必要學(xué)?該怎么學(xué)typescript

    typescript難學(xué)嗎?前端有必要學(xué)?該怎么學(xué)typescript

    TypeScript代碼與?JavaScript?代碼有非常高的兼容性,無(wú)門(mén)檻,你把?JS?代碼改為?TS?就可以運(yùn)行。TypeScript?應(yīng)該不會(huì)脫離?JavaScript?成為獨(dú)立的語(yǔ)言。學(xué)習(xí)?TypeScript?應(yīng)該主要指的是學(xué)習(xí)它的類(lèi)型系統(tǒng)。
    2022-12-12
  • postman數(shù)據(jù)加解密實(shí)現(xiàn)APP登入接口模擬請(qǐng)求

    postman數(shù)據(jù)加解密實(shí)現(xiàn)APP登入接口模擬請(qǐng)求

    對(duì)于Postman的使用,一般情況下只要發(fā)發(fā)確定的請(qǐng)求與參數(shù)就可以的了,然而,在使用的時(shí)候,尤其是接口測(cè)試時(shí),請(qǐng)求接口的設(shè)計(jì)里面都有數(shù)據(jù)加密,參數(shù)驗(yàn)簽,返回?cái)?shù)據(jù)也有進(jìn)行加密的,這個(gè)時(shí)候就需要使用一些腳本做處理,模擬app登入請(qǐng)求的操作
    2021-08-08
  • 簡(jiǎn)單三行代碼函數(shù)實(shí)現(xiàn)幾十行Typescript類(lèi)型推導(dǎo)

    簡(jiǎn)單三行代碼函數(shù)實(shí)現(xiàn)幾十行Typescript類(lèi)型推導(dǎo)

    這篇文章主要為大家介紹了簡(jiǎn)單三行代碼函數(shù)實(shí)現(xiàn)幾十行Typescript類(lèi)型推導(dǎo)的方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Manipulation-TypeScript?DOM操作示例解析

    Manipulation-TypeScript?DOM操作示例解析

    這篇文章主要為大家介紹了DOM?Manipulation-TypeScript?DOM操作示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • js 獲取今天以及過(guò)去日期

    js 獲取今天以及過(guò)去日期

    這篇文章主要介紹了js獲得當(dāng)前系統(tǒng)日期時(shí)間以及過(guò)去系統(tǒng)日期時(shí)間的方法,涉及javascript操作日期時(shí)間的相關(guān)技巧,示例代碼如下,需要的朋友可以參考下
    2017-04-04
  • d3-scale d3-scaleTime使用示例詳解

    d3-scale d3-scaleTime使用示例詳解

    這篇文章主要為大家介紹了d3-scale d3-scaleTime使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評(píng)論