vue - blog开发学习2

首页博客列表的开发

1、修改index.vue,使能够支持列表功能

<template>
  <div>
    <PostList v-for="(item,index) in postList" :title="item.title" :content="item.content" :postCount="postCount"
              :key="index"></PostList>
  </div>
</template>

<script>
  import PostList from ‘@/components/post-list‘

  export default {
    name: "index",
    components: {
      PostList
    },
    data() {
      return {
        postList: [
          {
            title: ‘测试1‘,
            content: ‘啊啊啊啊啊啊啊啊啊啊啊‘
          },
          {
            title: ‘测试1‘,
            content: ‘啊啊啊啊啊啊啊啊啊啊啊‘
          },
          {
            title: ‘测试1‘,
            content: ‘啊啊啊啊啊啊啊啊啊啊啊‘
          },
          {
            title: ‘测试1‘,
            content: ‘啊啊啊啊啊啊啊啊啊啊啊‘
          },
        ],
        postCount: 100
      }
    }

  }
</script>

<style scoped>

</style>

2、添加PostList自定义组件,自定义组件中使用到了iView中的Card标签

<template>
  <div style="background: #eee;padding: 1px;">
    <Card :bordered="false">
      <p>{{title}}</p>
      <p>{{content}}</p>
    </Card>
  </div>
</template>

<script>
  export default {
    name: "PostList",
    props: {
      title: {
        type: String,
        default: ‘‘
      },
      content: {
        type: String,
        default: ‘‘
      },
      postCount: {
        type: Number,
        default: 0
      }
    }
  }
</script>

<style scoped>

</style>

3、效果(访问根路径的话,会自动渲染index.vue中的内容,具体看router/index.js中的路由配置)

4、用router-link生成一个可点击的链接,使能够跳转到具体内容页面(post.vue)--页面还是渲染到home.vue中,修改post-list.vue组件,添加了一个router-link,这样,再点击每一块的时候都会跳转到post.vue中,并且会带着postId值

<template>
  <div style="background: #eee;padding: 1px; margin-left: 10%;margin-right: 10%">
    <router-link tag="div" :to="{name:‘Post‘,params:{postId:postId}}">
      <Card :bordered="false">
        <p>{{title}}</p>
        <p>{{content}}</p>
      </Card>
    </router-link>
  </div>
</template>

<script>
  export default {
    name: "PostList",
    props: {
      postId: {
        type: String,
        default: ‘‘
      },
      title: {
        type: String,
        default: ‘‘
      },
      content: {
        type: String,
        default: ‘‘
      },
      postCount: {
        type: Number,
        default: 0
      }
    },
    methods: {}
  }
</script>

<style scoped>

</style>

5、修改home页的menu标签,改成根据数据自动渲染菜单,zIndex这个是为了不覆盖菜单

<template>
  <div class="layout">
    <Layout>
      <Header :style="{position: ‘fixed‘, width: ‘100%‘,zIndex:‘900‘}">
        <Menu mode="horizontal" theme="dark" active-name="1">
          <div class="layout-logo">
            <img src="static/img/logo.png" alt="">
          </div>
          <div class="layout-nav">
            <MenuItem v-for="(item,index) in menuList" :name="index" :to="item.to" :key="index">
              <Icon :type="item.icon"></Icon>
              {{item.name}}
            </MenuItem>
          </div>
        </Menu>
      </Header>

      <Content :style="{margin: ‘88px 0 0‘, background: ‘#fff‘, minHeight: ‘500px‘}">
        <router-view></router-view>
      </Content>
      <Footer class="layout-footer-center">2011-2016 &copy; TalkingData</Footer>
    </Layout>
  </div>
</template>

<script>
  export default {
    name: "home",
    data() {
      return {
        menuList:[
          {
            name:‘首页‘,
            icon:‘ios-navigate‘,
            to:‘index‘
          },
          {
            name:‘类别‘,
            icon:‘ios-keypad‘,
            to:‘postClass‘
          },
          {
            name:‘新建‘,
            icon:‘ios-analytics‘,
            to:‘create‘
          },
          {
            name:‘修改‘,
            icon:‘ios-paper‘,
            to:‘edit‘
          },
          {
            name:‘关于我‘,
            icon:‘ios-paper‘,
            to:‘aboutMe‘
          }
        ]
      }
    }
  }
</script>

<style scoped>
  @import "../static/css/home.css";

</style>

6、首页基本功能写完后,使用mock模拟后台返回的json数据,此方式基于axios发送请求方式,因此安装axios

①npm install axios --save

②安装完成之后,将axios绑定为全局函数,因此在main.js中添加

import axios from ‘axios‘

Vue.prototype.$axios = axios

使用时: 

this.$axios.get(‘api/getNewsList‘)
.then((response)=>{
    this.newsList=response.data.data;
}).catch((response)=>{
    console.log(response);
})

或者

import axios from ‘axios‘
import VueAxios from ‘vue-axios‘

Vue.use(VueAxios,axios);

使用:
this.axios.get(‘api/getNewsList‘).then((response)=>{
        this.newsList=response.data.data;
      }).catch((response)=>{
        console.log(response);
      })

③安装mockjs

npm install mockjs --save-dev   //开发使用  所以加上dev

④修改main.js,添加自定义的mock.js(http://mockjs.com/

https://blog.csdn.net/xiaoxiaojie12321/article/details/81301399

require(‘./mock/mock.js‘)

⑤自定义mock.js

//引入mockjs
const Mock = require(‘mockjs‘)
// 获取mock.Random对象
const Random = Mock.Random
//mock数据
const data = () => {
  let posts = []
  for (let i = 0; i < 50; i++) {
    let post = {
      title: Random.csentence(5, 30),
      content: Random.csentence(4000, 5000)
    }
    posts.push(post)
  }

  return {
    posts: posts
  }
}

Mock.mock(‘/api/posts‘,‘get‘,data)

⑥修改index.vue中的获取数据的方式

    created() {
      this.$axios({
        url: ‘/api/posts‘,
        method: ‘get‘
      }).then(response => {
        this.postList = response.data.posts
        console.log(this.postList)
      })
    }

⑦添加bootstrap支持

npm install bootstrap jquery --save

webpack.base.conf.js

  plugins:[
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "windows.jQuery": "jquery"
    })
  ],

main.js

//boostrap
import $ from ‘jquery‘
import ‘bootstrap/dist/css/bootstrap.min.css‘
import ‘bootstrap/dist/js/bootstrap.min.js‘

整体:

点击每一个框:

原文地址:https://www.cnblogs.com/nxzblogs/p/10923014.html

时间: 2024-08-06 13:27:52

vue - blog开发学习2的相关文章

vue - blog开发学习1

1.安装vue-cli vue intall -g vue-cli 2.创建项目 vue init webpack nblog 3.按提示要求配置项目 ? Project name nblog ? Project description 学习bolg开发 ? Author nxzJIA <987097855@qq.com> ? Vue build standalone ? Install vue-router? Yes ? Use ESLint to lint your code? Yes ?

vue - blog开发学习5

基本功能和后台联调 1.首页的所有博客 因为是前后台都是本地开发,所以前端vue需要设置proxy:修改/config/index.js中的这个proxyTable proxyTable: { '/api': { target: 'http://localhost:8888', changeOrigin: true, pathRewrite: { '^/api': '' } }, 同时将mock.js中的模拟数据方法注释掉 2.后台添加cors package com.nxz.blog.conf

vue - blog开发学习6

1.问题,如下图,使用iviewui中的card导致页面不能出现滚动条(不太会弄,在网上查了一个vue组件vuescroll,因此使用这个做滚动条) 2.安装vuescroll cnpm install -S vuescroll https://vuescrolljs.yvescoding.org/zh/guide/getting-started.html#%E5%AE%89%E8%A3%85 3.问题:项目使用的jpa操作数据库,因为postclass和post是多堆多的关系,因此post.j

web前端开发学习课程大纲路线图及学习方法分享

想学好web前端开发,要学会阅读别人优秀的代码.web前端开发思想并不是统一固定不变的,阅读别人代码的过程就是间接的在向别人学习,这一过程中可以学习别人的开发思路,不同的人思路是不一样的,如果别人写的代码很优秀.很简单.且运行和性能上有很大的优势,就有很多可以借鉴的地方. 以下这份web前端学习路线图适合所以零基础的学员学习,都是从浅入深,没有基础的同学跟着视频教程及课程大纲一步一步的学习是可以很好的掌握的. 那么想要学好html5前端开发,那么需要掌握的专业技术有: 第一阶段:前端页面重构 内

Android开发学习之路--网络编程之xml、json

一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载xampp,然后安装之类的就不再多讲了,参考http://cnbin.github.io/blog/2015/06/05/mac-an-zhuang-he-shi-yong-xampp/.安装好后,启动xampp,之后在浏览器输入localhost或者127.0.0.1就可以看到如下所示了: 这个就

Android开发学习---使用XmlPullParser解析xml文件

Android中解析XML的方式主要有三种:sax,dom和pull关于其内容可参考:http://blog.csdn.net/liuhe688/article/details/6415593 本文将主要介绍pull解析器解析xml文件,环境为ubuntu 12.04+ intelij 13.1 + android sdk 2.1 一.创建一个XML项目,步骤如下: 二.解析一个xml文件: assets/person.xml <?xml version="1.0" encodi

.net 网站开发学习资源

慕课网 前端基础学习 http://www.imooc.com/course/list?c=fe 了解需求 例子之一 http://wenku.it168.com/d_000517899.shtml mvc教程 http://blog.csdn.net/powertoolsteam/article/details/47609257 asp.net 的使用类大全 http://git.oschina.net/kuiyu/dotnetcodes/blob/master/DotNet.Utilitie

[Android游戏开发学习笔记]View和SurfaceView

本文为阅读http://blog.csdn.net/xiaominghimi/article/details/6089594的笔记. 在Android游戏中充当主要角色的,除了控制类就是显示类.而在Android中涉及到显示的是View类,及继承自它的SurfaceView类和SurfaceView的其他子类等. 这里先只说View和SurfaceView.SurfaceView的直接子类有GLSurfaceView和VideoView,可以看出GL和视频播放以及CAmera摄像头一般均使用Su

Vue.js 基础学习

今天我开始了Vue.js 的学习. 那么什么是Vue.js 呢? Vue.js是一套开发Web页面的JavaScript脚本框架.听起来感觉很难,不过据说,Vue.js是Web-Javascript脚本框架中最容易上手的框架.所以我便选择了先来学习这个. 要学习Vue.js首先就要获取库文件了,在网上有很多地方可以找到,我是在bootcdn上找到的 接下来我们通过Vue输出一串Hello World ! 首先引入vue. <script src="https://cdn.bootcss.c