Vue github用户搜索案例分享

下面我会详细讲解“Vue github 用户搜索案例分享”的完整攻略并附带两条示例说明。

简介

本次分享的案例是一个基于 Vue.js 的 Github 用户搜索应用,借助 Github 的公共 API 实现了在搜索框中输入用户名后可查看该用户的 Github 账号信息以及其仓库列表。

技术栈

  • Vue.js:构建用户界面的 MVVM 框架,核心思想是响应式编程和组件化编程思想。
  • Axios:基于 Promise 的 HTTP 请求库,可以用在浏览器和 Node.js 中。
  • GitHub API:可使用 Github 提供的 RESTful API 获取 Github 官方数据。

步骤

步骤 1:初始化项目

我们首先在终端中进入项目所在目录,使用 Vue cli 创建项目:

vue create vue-github-users-search

根据提示配置好相关选项即可。

步骤 2:修改 App.vue

在项目中,打开 src/components/App.vue 文件,在 <template> 标签中添加以下代码:

<div>
  <h1>GitHub Users Search</h1>
  <input
    type="text"
    placeholder="Enter GitHub username"
  />
  <button>Search</button>
</div>

我们创建了一个搜索框和一个按钮,并将其包裹在一个 <div> 标签中。

步骤 3:实现搜索功能

我们将在组件的 methods 属性中实现搜索功能。

首先引入 axios,并创建 searchUser() 方法:

import axios from 'axios';

...

methods: {
  async searchUser() {
    const username = 'octocat';
    try {
      const response = await axios.get(
        `https://api.github.com/users/${username}`
      );
      console.log(response.data);
    } catch (error) {
      console.error(error);
    }
  }
}

这里先使用了 asyncawait 实现异步操作。

然后我们调用 axios.get(),发出 GET 请求并传入用户名作为 URL 的一部分,以获取 Github 用户信息。我们用 console.log() 打印响应数据以查看是否成功。

最后,我们在模板中将 searchUser() 绑定到搜索按钮上:

<button @click="searchUser()">Search</button>

现在,当我们点击按钮时,控制台会将 Github 用户信息打印出来。

步骤 4:显示用户信息

为了在模板中显示用户信息,我们需要在组件中创建 data() 方法,并在其中创建两个空对象,一个用于存储用户信息,另一个用于存储仓库列表。

data() {
  return {
    user: {},
    repos: [],
  };
}

searchUser() 方法中,我们使用响应数据更新 user 对象。修改 methods 属性如下:

methods: {
  async searchUser() {
    const username = 'octocat';
    try {
      const response = await axios.get(
        `https://api.github.com/users/${username}`
      );
      this.user = response.data;
    } catch (error) {
      console.error(error);
    }
  }
}

在模板中,我们使用插值语法来显示用户信息:

<div v-if="user.login">
  <h2>{{ user.login }}</h2>
  <img v-bind:src="user.avatar_url" alt="Avatar" />
  <p>{{ user.bio }}</p>
  <p>{{ user.location }}</p>
</div>

注意,由于 searchUser() 方法是异步的,因此我们需要在 div 元素上添加 v-if="user.login",以确保只有在用户存在时才会显示用户信息。

步骤 5:显示仓库列表

我们将使用与搜索用户相同的方法来获取用户的仓库列表。

在组件中创建 searchRepos() 方法:

async searchRepos() {
  const username = 'octocat';
  try {
    const response = await axios.get(
      `https://api.github.com/users/${username}/repos`
    );
    this.repos = response.data;
  } catch (error) {
    console.error(error);
  }
},

在模板中,我们使用 v-for 来显示每个仓库。

<ul>
  <li v-for="repo in repos" :key="repo.id">
    <a :href="repo.html_url">{{ repo.name }}</a>
    <p>{{ repo.description }}</p>
    <p>Language: {{ repo.language }}</p>
  </li>
</ul>

最后,我们将 searchRepos() 方法绑定到搜索按钮上:

<button @click="searchUser(); searchRepos()">Search</button>

示例 1:使用 Vuex 管理状态

我们可以使用 Vuex 来管理组件的状态。在 src 目录下创建一个名为 store.js 的文件,并在其中创建一个 store:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: {},
    repos: [],
  },
  mutations: {
    updateUser(state, user) {
      state.user = user;
    },
    updateRepos(state, repos) {
      state.repos = repos;
    },
  },
  actions: {
    async fetchUserProfile({ commit }, username) {
      try {
        const response = await axios.get(
          `https://api.github.com/users/${username}`
        );
        commit('updateUser', response.data);
      } catch (error) {
        console.error(error);
      }
    },
    async fetchUserRepos({ commit }, username) {
      try {
        const response = await axios.get(
          `https://api.github.com/users/${username}/repos`
        );
        commit('updateRepos', response.data);
      } catch (error) {
        console.error(error);
      }
    },
  },
});

然后在组件中引入 store,并将 data() 方法中的数据移除,由 store 提供:

import store from '../store';

然后,在组件中使用 computed 属性来获取 store 中的状态:

computed: {
  user() {
    return this.$store.state.user;
  },
  repos() {
    return this.$store.state.repos;
  },
},

最后,我们修改 methods 属性来调用 action:

methods: {
  async searchUser() {
    const username = 'octocat';
    await this.$store.dispatch('fetchUserProfile', username);
  },
  async searchRepos() {
    const username = 'octocat';
    await this.$store.dispatch('fetchUserRepos', username);
  },
}

示例 2:使用 Vue Router 导航

我们可以利用 Vue Router 实现路由导航,例如在搜索后跳转到用户详情页。

首先,在 src 目录下创建一个名为 router.js 的文件,并在其中新建一个 router:

import Vue from 'vue';
import VueRouter from 'vue-router';

import Users from './components/Users.vue';
import User from './components/User.vue';

Vue.use(VueRouter);

export default new VueRouter({
  mode: 'history',
  routes: [
    { path: '/', component: Users },
    { path: '/:username', component: User },
  ],
});

这里我们创建了两个路由:/ 是主页,显示搜索框和仓库列表;/:username 是用户详情页,显示用户信息和仓库列表。

然后在 main.js 中引入 router,并将其绑定到根实例上:

import router from './router';

new Vue({
  router,
  render: h => h(App),
}).$mount('#app');

最后,在组件中添加路由跳转链接,例如在搜索成功后跳转到用户详情页:

<a v-if="user.login" :to="`/${user.login}`">{{ user.login }}</a>

这就是“Vue Github 用户搜索案例分享”的完整攻略并附带了两条示例说明。如果需要更加详细的代码可以参考Github

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vue github用户搜索案例分享 - Python技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • Visual Studio 2019中使用Git

    下面我将详细讲解“Visual Studio 2019中使用Git”的完整攻略,包含两条示例说明。 一、准备工作 在开始使用Git之前,需要进行一些准备工作,包括安装Git和配置GitHub账号等操作: 1. 安装Git 在Windows系统中,可以在Git官网 https://git-scm.com/downloads 下载最新版的Git安装包,双击安装即…

    GitHub 2023年5月16日
    00
  • git标签管理_动力节点Java学院整理

    Git标签管理攻略 Git标签是Git代码版本管理中的一个重要功能。标签可以被认为是一个稳定版本的快照,它可以用来表示代码的里程碑,是一个特定时间点的代码快照,常用于版本发布、发布稳定版本等操作中。 Git标签的基本命令 以下是Git标签管理中常用的基本命令: 1. 创建标签 在Git中,标签分为两种:轻量标签(lightweight)和附注标签(annot…

    GitHub 2023年5月16日
    00
  • GitHub倡导的CSS编写风格及文件目录部署指南

    GitHub倡导的CSS编写风格及文件目录部署指南主要是指在编写CSS时应该遵循一些规范和约定俗成的标准,以便于代码的维护和可读性的提高。同时,对于文件目录的部署也需要有一些规则,以便于后期的管理和扩展。 CSS编写风格: 缩进 在CSS中一般使用2或4个空格进行缩进,而不是使用Tab键。 样式规则 样式规则中一般每行只包含一条属性值对,属性和值之间应该用一…

    GitHub 2023年5月16日
    00
  • chatGPT本地部署、运行和接口调用的详细步骤

    下面是关于chatGPT本地部署、运行和接口调用的详细步骤攻略: 1. 环境准备 安装anaconda和pytorch 首先需要安装anaconda和pytorch(根据自己的操作系统和CUDA版本选择相应的安装包),可以参考PyTorch官网(https://pytorch.org/get-started/locally/)进行安装。 安装transfor…

    GitHub 2023年5月16日
    00
  • 在vscode中使用Git的教程

    使用Git管理代码是现代软件开发的标配之一。在Visual Studio Code (VS Code)中使用Git能够方便地进行代码管理、版本控制、协同开发等操作。接下来,我们将为你详细介绍如何在VS Code中使用Git。 一、安装Git 在使用Git前,首先需要在本地安装Git。你可以前往Git官网(https://git-scm.com/)下载对应系统…

    GitHub 2023年5月16日
    00
  • vscode安装git及项目开发过程

    下面我将详细讲解如何在VSCode中安装Git以及使用它进行项目开发的过程。 安装Git 在官网下载并安装Git:https://git-scm.com/downloads 安装完成之后,打开VSCode,先确保你已经安装了最新版的VSCode。 点击左侧的图标,打开Extensions面板,搜索并安装Git扩展。 点击左侧Git图标,在输入框中输入你的用户…

    GitHub 2023年5月16日
    00
  • vue devtools的安装与使用教程

    Vue DevTools是一个强大的浏览器扩展程序,可以帮助我们开发和调试Vue.js程序。下面是Vue DevTools的安装和使用教程: 安装Vue DevTools 首先,我们需要使用Vue CLI创建一个新的Vue项目。 在安装Vue CLI时,可以选择添加Vue DevTools插件。如果没有安装,可以使用以下命令在项目中安装: npm insta…

    GitHub 2023年5月16日
    00
  • 微信小程序(应用号)简单实例应用及实例详解

    微信小程序(应用号)简单实例应用及实例详解 简介 微信小程序是一种全新的开发模式,可以在微信中使用的应用程序。相较于传统的Web应用,微信小程序能够更加便捷地获取用户资源,同时也具备了更好的用户体验。 准备工作 要进行小程序开发,首先需要准备好下面的工作: 微信公众平台账号 微信小程序开发工具 示例一:数字彩票 实现方案 实现一个数字彩票应用,用户可以自主选…

    GitHub 2023年5月16日
    00
合作推广
合作推广
分享本页
返回顶部