老弟,来了?VUE+Nuxt.js+Koa+Vuex入门教程(一)仿写一个cnode网站

本文主要是介绍老弟,来了?VUE+Nuxt.js+Koa+Vuex入门教程(一)仿写一个cnode网站,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

if(有工作){if(工作地址 ==  "深圳" || 工作地址 ==  "广州" ){do(请联系作者,qq:1172081598)}
}

何为Nuxt.js

Nuxt.js是一个vue的服务端渲染的框架,集成了express框架,sass/less框架等等,ui框架如Bootstrap,Vuetify,Bulma,Tailwind,Element UI,Ant Design Vue,Buefy,方便的集成拓展其他框架,如EsLint等等,自动化打包,代码改动自动更新(服务器,前端代码),让开发变得简单。

开始安装

文档地址在这: nuxt.js
首先你必须安装了node环境, npm(或者 yarn);

 npx create-nuxt-app <项目名>

安装到中间的时候, 会让你选择开发的框架
ui框架我选择了element-ui, 服务器框架选择了koa,还有可以选择预编译样式框架sass / less;
选择完成之后就会进行安装,需要等待一段时间。(如果没有v皮N的话,建议选择淘宝源,进行npm安装module的时候会快很多)
$ npm config set registry https://registry.npm.taobao.org
– 配置后可通过下面方式来验证是否成功

$ npm config get registry
-- 或 npm info express

然后我们看到nuxt.config.js, 这个是nuxt项目的配置文件,

const pkg = require('./package')module.exports = {mode: 'universal',/*** Headers of the page*/head: {title: pkg.name,meta: [{ charset: 'utf-8' },{ name: 'viewport', content: 'width=device-width, initial-scale=1' },{ hid: 'description', name: 'description', content: pkg.description }],link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]},/*** Customize the progress-bar color*/loading: { color: '#fff' },/*** Global CSS 全局的css,公共的css引用也在这*/css: ['element-ui/lib/theme-chalk/index.css'],/*** Plugins to load before mounting the App*/plugins: ['@/plugins/element-ui'],/*** Nuxt.js modules*/modules: [// Doc: https://github.com/nuxt-community/axios-module#usage'@nuxtjs/axios'],/*** Axios module configuration*/axios: {// See https://github.com/nuxt-community/axios-module#options},/*** Build configuration*/build: {/*** webpack 的配置项在这里拓展**比如启用eslint*/extend(config, ctx) {// Run ESLint on save// if (ctx.isDev && ctx.isClient) {// config.module.rules.push({//   enforce: 'pre',//   test: /\.(js|vue)$/,//   loader: 'eslint-loader',//   exclude: /(node_modules)/// })// }}}
}

然后我们看pages/index.vue,我们记住vue里面template只有一个根元素,如果有多个就会报错,

<template><div><head1/><div class="m" v-loading.lock='loading'><div class="main"><div class="content"><div class="pannel"><div class="b_head"><a class="currentTab tab" href="/?tab=share">分享</a><a class="tab" href="/?tab=ask">问答</a><a class="tab" href="/?tab=job">招聘</a><a class="tab" href="/?tab=good">精华</a></div></div><div class="secDiv" v-loading.lock="loading"><div v-for="item of articleListsData" :key='item.length' class="cell"><nuxt-link  :to="item.author.avatar_url" :title='item.author.loginname'><img :src="item.author.avatar_url" :title='item.author.loginname' class="headImg"></nuxt-link><span class="count_reply pull_left"><span class="count_replyNum">{{item.reply_count}}</span><span>/</span><span class="visit_count">{{item.visit_count}}</span></span><div class="topic_title_wrapper textD"><span class="put_top" v-if="item.top">置顶</span><span class="topiclist-tab" v-else-if="item.tab">{{item.tab == 'ask' ? '问答': item.tab == 'share'? '分享': '' }}</span><nuxt-link :to='"/topic/" + item.id' class="topic_title">{{item.title}}</nuxt-link></div><span class="pull_right"><span class="last_active_time">{{dealTime(item.create_at)}}</span></span></div></div></div><div class="aside">{{ip}}</div></div></div><div class="next"><input type="button" value="下一页" @click="getData()"></div></div></template><script>import Logo from '~/components/Logo.vue';import head1 from '~/components/header.vue';// import body1 from '~/components/body.vue';import axios from 'axios'import { mapState } from 'vuex'export default {components: {Logo,head1,},transition: 'page',data() {return {loading: true,ip: ''}},computed: mapState(['articleLists']),asyncData(context){return axios.get('https://cnodejs.org/api/v1/topics?pages=1&limit=30&mdrender=false').then(res => ({articleListsData: res.data.data,loading: false})).catch(res =>{throw new Error('Maisec.vue: ', res)})},methods: {async fetchSomething() {const ip = await this.$axios.$get('http://icanhazip.com')this.ip = ip},getData (){this.fetchSomething()this.$store.dispatch('getArticleLists')},dealTime(time){let now = (new Date()).getTime();time = (new Date(time)).getTime();let res = (now-time)/1000,str = '';if (res/(60*60*24) > 1){str = Math.floor(res/(60*60*24)) + '天前'}else if (res/(60*60) > 1){str = Math.floor(res/(60*60)) + '小时前'}else{str = Math.floor(res/(60)) + '分钟前'}return str}},watch:{articleLists(val){this.articleListsData = this.articleLists}}}
</script><style scoped type="text/css">body{background: #e5e5e5;}a{text-decoration: none; }.secDiv .cell:hover{background: #e5e5e5;}.secDiv .cell:nth-child(1){border-top: none;}.secDiv .inner .unstyled li div, .topic_title_wrapper, .user_name, a.dark, a.topic_title{text-overflow: ellipsis;}.count_reply{width: 70px;display: inline-block;text-align: center;}.cell{padding-right: 10px;border-top: 1px solid #ccc;background: #fff;position: relative;padding: 10px 0 10px 10px;font-size: 14px;}.last_active_time{display: inline-block;min-width: 50px;color: #999;white-space: nowrap;}.pull_right{float: right;}.topiclist-tab{background: #e5e5e5;color: #999;padding: 2px 4px;border-radius: 3px;font-size: 12px;}.put_top{padding: 2px 4px;background: #80bd01;border-radius: 3px;color: #fff;font-size: 12px;}.m{width: 100%;display:flex;justify-content: center;}.main{width: 90%; min-height: 400px; max-width: 1400px; height: 957px; margin: 15px auto;}.content{padding: 0;margin-right: 305px;}.b_head{width: 100%; padding: 10px;background: #f6f6f6; border-radius: 3px 3px 0 0;}.b_head  a{color: #80bd01;}.tab{margin: 0 10px; font-size: 14px;}.b_head .currentTab{background: #80bd01; color: #fff; padding: 3px 4px; border-radius: 3px; }.headImg{width: 30px;height: 30px;border-radius: 3px;}.aside{ width:290px; font-size: 14px; float: right;}.container {min-height: 100vh;}.title {font-family: 'Quicksand', 'Source Sans Pro', -apple-system, BlinkMacSystemFont,'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;display: block;font-weight: 300;font-size: 100px;color: #35495e;letter-spacing: 1px;
}.subtitle {font-weight: 300;font-size: 42px;color: #526488;word-spacing: 5px;padding-bottom: 15px;
}.links {padding-top: 15px;
}.topic_title_wrapper{white-space: nowrap; 
}.textD{display: inline-block;
}
</style>

我们把头部导航栏写成组件, 在components文件夹新建一个文件header.vue

<template><div><div class="navbar"><div class="navInner"><div class="navContainer"><div class="logo"><a href="/"><img src="../static/img/cnodejs_light.svg" class="navLogo"> </a></div><div class="about"><a href="/about">关于</a></div></div></div></div></div>
</template><style type="text/css" scoped="scoped">.navbar{height: 50px; line-height: 50px; font-size: 13px;  background: #444; width: 100%; color: #80bd01;
}	.navInner{width: 90%; margin: auto;}.navInner a{color: #fff;text-decoration: none;}.navContainer{width: 100%;min-width: 960px;margin: 0 auto;display: flex;justify-content: space-around;
}.logo{height: 100%;
}@media screen and (max-width: 979px ){.navInner{width: 100%}
}
.navLogo{width: 128px; height: 28px;
}
</style>

在store文件夹下新建actions.js:
主要是处理获取数据的函数

import axios from 'axios'export default{getArticleLists(context) {context.commit('addArticleNumber')const number = context.getters.getArticleNumberaxios.get(`https://cnodejs.org/api/v1/topics?pages=1&limit=${number}&mdrender=false`).then(res =>{context.commit('addArticleLists', res.data.data)}).catch(res =>{throw new Error('err: ', res)})}
}

store下新建getters.js:
主要是暴露函数

export default {getArticleLists: state => state.articleLists,getArticleNumber: state => state.articleNumber,getArticle: state => state.article,getArticleAuthor: state => state.articleAuthor,getUserInfo: state => state.userInfo
}

在store下新建index.js:
主要是初始化Vuex:

import Vuex from 'vuex'
import mutations from './mutations'
import actions from './actions'
import getters from './getters'
import state from './state'const createStore = ()=> new Vuex.Store({state,getters,mutations,actions
})export default createStore

在store下新建mutations.js:
这里主要是为了改变vuex里面的数值

export default{addArticleLists(state, articleLists){state.articleLists = articleLists},addArticleNumber(state){state.articleNumber += 10}
}

接着, 使用命令行:

npm run dev

就可以在浏览器打开http://127.0.0.1:3000查看效果了
在这里插入图片描述

这篇关于老弟,来了?VUE+Nuxt.js+Koa+Vuex入门教程(一)仿写一个cnode网站的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136830

相关文章

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

JS 实现复制到剪贴板的几种方式小结

《JS实现复制到剪贴板的几种方式小结》本文主要介绍了JS实现复制到剪贴板的几种方式小结,包括ClipboardAPI和document.execCommand这两种方法,具有一定的参考价值,感兴趣的... 目录一、Clipboard API相关属性方法二、document.execCommand优点:缺点:

nginx部署https网站的实现步骤(亲测)

《nginx部署https网站的实现步骤(亲测)》本文详细介绍了使用Nginx在保持与http服务兼容的情况下部署HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录步骤 1:安装 Nginx步骤 2:获取 SSL 证书步骤 3:手动配置 Nginx步骤 4:测

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

css渐变色背景|<gradient示例详解

《css渐变色背景|<gradient示例详解》CSS渐变是一种从一种颜色平滑过渡到另一种颜色的效果,可以作为元素的背景,它包括线性渐变、径向渐变和锥形渐变,本文介绍css渐变色背景|<gradien... 使用渐变色作为背景可以直接将渐China编程变色用作元素的背景,可以看做是一种特殊的背景图片。(是作为背

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要