和风天气的使用方法

方法一 组件使用

 <div id="he-plugin-simple"></div>
 // 初始化和风天气
onBeforeMount(() => {
  window.WIDGET = {
    CONFIG: {
      modules: "21034",
      background: "5",
      tmpColor: "FFFFFF",
      tmpSize: "18",
      cityColor: "FFFFFF",
      citySize: "18",
      aqiColor: "FFFFFF",
      aqiSize: "18",
      weatherIconSize: "45",
      alertIconSize: "18",
      padding: "10px 10px 10px 10px",
      shadow: "0",
      language: "auto",
      fixed: "false",
      vertical: "center",
      horizontal: "left",
      key: "你自己的key",
    },
  };
  const script = document.createElement("script");
  script.type = "text/javascript";
  script.src =
    "https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0";
  document.getElementsByTagName("head")[0].appendChild(script);
});

方法二 接口调用

import axios from "axios";
// 天气
let location = reactive({
  now: {},
});
// 空气
let air = reactive({
  now: {},
});
onBeforeMount(() => {
  axios
    .get(
      "https://devapi.qweather.com/v7/weather/now?location=坐标点&key=自己的key"
    )
    .then((response) => (location.now = response.data.now));
  // 空气
  axios
    .get(
      "https://devapi.qweather.com/v7/air/now?location=坐标点&key=自己的key"
    )
    .then((response) => (air.now = response.data.now));
});
Vite + Vue 3 + TypeScript 的项目中,你可以使用 Vue Router 来处理路由调用接口。 首先,确保你已经安装了 Vue Router 和 axios(或其他用于发送 HTTP 请求的库)。你可以使用以下命令进行安装: ``` npm install vue-router axios ``` 接下来,在你的项目中创建一个 `router` 文件夹,在其中创建一个 `index.ts` 文件。在该文件中,你可以配置你的路由。以下是一个示例: ```typescript import { createRouter, createWebHistory, RouteRecordRaw } from &#39;vue-router&#39;; import Home from &#39;@/views/Home.vue&#39;; import About from &#39;@/views/About.vue&#39;; const routes: Array<RouteRecordRaw> = [ { path: &#39;/&#39;, name: &#39;Home&#39;, component: Home, }, { path: &#39;/about&#39;, name: &#39;About&#39;, component: About, }, ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` 在上面的示例中,我们定义了两个路由:`Home` 和 `About`。你可以根据你的需求进行修改和扩展。 然后,在你的入口文件(如 `main.ts`)中,引入使用你的路由: ```typescript import { createApp } from &#39;vue&#39;; import App from &#39;./App.vue&#39;; import router from &#39;./router&#39;; const app = createApp(App); app.use(router); app.mount(&#39;#app&#39;); ``` 现在,你可以在你的组件中使用 `vue-router` 进行路由导航和调用接口。例如,在 `Home.vue` 组件中,你可以这样使用: ```vue <template> <div> <h1>Home</h1> <button @click="fetchData">Fetch Data</button> </div> </template> <script> import axios from &#39;axios&#39;; export default { methods: { fetchData() { axios.get(&#39;/api/data&#39;).then((response) => { console.log(response.data); }).catch((error) => { console.error(error); }); }, }, }; </script> ``` 在上面的示例中,我们使用了 axios 发送 GET 请求来获取数据。你可以根据你的需求调整和扩展这个例子。 希望这能帮助到你!如果你有任何其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值