以下实例代码地址:https://github.com/NewBLife/VueDev

1,Vue组件导入

新建组件:Header.vue

<template>
<div>
<p class="title">{{title}}</p>
</div>
</template> <script>
export default {
data:function(){
return{
title:'This is Header component'
}
}
}
</script> <style>
.title{
font-size: 20px;
font-weight: bold;
}
</style>

导入组件:

<template>
<div>
<header-component></header-component>
<p class="blue">This is App component.</p>
</div>
</template> <script>
import Header from './Header.vue'; export default {
components:{
'header-component':Header
}
}
</script> <style>
.blue{
color: blue;
}
</style>

2,Index.html文件复制

npm i html-webpack-plugin --save

// webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = {
mode: 'development',
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html'
})
],

3,Jquery导入

npm i jquery --save-dev

//webpack.config.js
const webpack = require('webpack'); module.exports = {
mode: 'development',
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html'
})
],

使用:

<template>
<div>
<my-header></my-header>
<p class="red" v-if="msg.length>0">
{{msg}}
</p>
<p v-else>
no text
</p>
<input type="text" v-model="msg">
<button @click="clear()">clear</button>
</div>
</template> <script>
import myHeader from './components/myheader.vue'; export default {
components:{
'my-header':myHeader
},
data:function(){
return {
msg :"Hello World"
}
},
methods:{
clear:function(){
this.msg =''
} },
// ready
mounted: function () {
this.$nextTick(function () {
var that = this
$.getJSON('http://www.geonames.org/postalCodeLookupJSON?postalcode=10504&country=US&callback=?', {}, function (json) {
console.log(json)
that.msg = json.postalcodes[0].adminName1
})
})
}
}
</script> <style>
.red{
color:#f00;
}
</style>

4,Vue-router导入

router.js定义

import Top from './pages/top';
import About from './pages/about';
import Contract from './pages/contract';
import Userinfo from './pages/userinfo';
const UserProfile = { template: '<div>Profile</div>' }
const UserPosts = { template: '<div>Posts</div>' }
const UserPostsHelp = { template: '<div>Posts Help</div>' } export default [{
path: '/',
component: Top
},
{
path: '/about',
component: About
},
{
path: '/contract',
component: Contract
},
{
path: '/user/:userId',
name: 'user',
component: Userinfo,
children: [{
path: 'profiles',
name: 'userprofile',
component: UserProfile
},
{
path: 'posts',
name: 'userpost',
components: {
default: UserPosts,
helper: UserPostsHelp
}
}
]
}
]

引用

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter); import routes from './router'
const router = new VueRouter({
mode: 'history',
routes: routes
}); const app = new Vue({
el: '#app',
router
});

App.vue设置路由

<template>
<div>
   <div class="header">
<router-link to="/">
top
</router-link>
<router-link to="about">
about
</router-link>
<router-link to="contact">
contact
</router-link>
</div>
<div class="content">
<router-view></router-view>
</div>
</div>
</template>

5,API访问

Axios方式(引入axios包)

<template>
<div>
<h1>Bitcoin Price Index</h1> <section v-if="errored">
<p>We're sorry, we're not able to retrieve this information at the moment, please try back later</p>
</section> <section v-else>
<div v-if="loading">Loading...</div> <div v-else v-for="currency in info" class="currency">
{{ currency.description }}:
<span class="lightena">
<span v-html="currency.symbol"></span>{{ currency.rate_float | currencydecimal }}
</span>
</div> </section>
</div>
</template> <script>
import axios from 'axios'; export default {
data(){
return {
info:null,
loading:true,
errored:false
}
},
filters:{
currencydecimal(value){
return value.toFixed(2)
}
},
mounted() {
axios.get('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(response=>{
this.info = response.data.bpi
})
.catch(error=>{
console.log(error)
this.errored =true
})
.finally(()=>this.loading=false)
},
}
</script> <style>
.lightena {
color: blue;
}
</style>

FetchAPI方式:不需要引用特殊的包。

<template>
<div>
<h1>Bitcoin Price Index</h1> <section v-if="errored">
<p>We're sorry, we're not able to retrieve this information at the moment, please try back later</p>
</section> <section v-else>
<div v-if="loading">Loading...</div> <div v-else v-for="currency in info" class="currency">
{{ currency.description }}:
<span class="lighten">
<span v-html="currency.symbol"></span>{{ currency.rate_float | currencydecimal }}
</span>
</div> </section>
</div>
</template> <script>
export default {
data(){
return {
info:null,
loading:true,
errored:false
}
},
filters:{
currencydecimal(value){
return value.toFixed(2)
}
},
mounted() {
fetch('https://api.coindesk.com/v1/bpi/currentprice.json', {
method: 'GET',
mode: 'cors'
})
.then(response=>{
return response.json();
})
.then(response=>{
this.info= response.bpi
})
.catch(error=>{
console.log(error)
this.errored =true
})
.finally(()=>this.loading=false)
},
}
</script> <style>
.lighten {
color: red;
}
</style>

Vue基础开发笔记的更多相关文章

  1. Vue基础开发入门之简单语法知识梳理(思维导图详解)

    基于个人写的以下关于Vue框架基础学习的三篇随笔,在此基础上,做一个阶段性的知识总结,以此来检验自己对Vue这一段时间学习的成果,内容不多,但很值得一看.(思维导图详解)

  2. Vue基础入门笔记

    不是面向DOM进行编程,而是面向数据去编程.当数据发生改变,页面就会随着改变. 属性绑定(v-bind)和双向数据绑定(v-model) 模板指令(v-bind:)后面跟的内容不再是字符串而是: js ...

  3. J2EE--Struts2基础开发笔记

    内容中包含 base64string 图片造成字符过多,拒绝显示

  4. 两万字Vue.js基础学习笔记

    Vue.js学习笔记 目录 Vue.js学习笔记 ES6语法 1.不一样的变量声明:const和let 2.模板字符串 3.箭头函数(Arrow Functions) 4. 函数的参数默认值 5.Sp ...

  5. python 全栈开发,Day89(sorted面试题,Pycharm配置支持vue语法,Vue基础语法,小清单练习)

    一.sorted面试题 面试题: [11, 33, 4, 2, 11, 4, 9, 2] 去重并保持原来的顺序 答案1: list1 = [11, 33, 4, 2, 11, 4, 9, 2] ret ...

  6. Linux及Arm-Linux程序开发笔记(零基础入门篇)

    Linux及Arm-Linux程序开发笔记(零基础入门篇)  作者:一点一滴的Beer http://beer.cnblogs.com/ 本文地址:http://www.cnblogs.com/bee ...

  7. 【Linux开发】Linux及Arm-Linux程序开发笔记(零基础入门篇)

    Linux及Arm-Linux程序开发笔记(零基础入门篇) 作者:一点一滴的Beer http://beer.cnblogs.com/ 本文地址:http://www.cnblogs.com/beer ...

  8. TERSUS无代码开发(笔记01)-按装下载和基础语法

    1.中国官网 https://tersus.cn/ 2.下载:https://tersus.cn/download/ 3.开发文档:https://tersus.cn/docs/ 4.基本元件说明 图 ...

  9. 两万字Vue.js基础学习笔记(二)

    Vue.js学习笔记(二) 4.模块化开发 ES6模块化的导入和导出 我们使用export指令导出了模块对外提供的接口,下面我们就可以通过import命令来加载对应的这个模块了 首先,我们需要在HTM ...

随机推荐

  1. 关于indexof和substring经常记不住的点

    indexof 找到的字符位置是 字符串从0位开始算起的. lastIndexOf也一样,http://localhost:8080/aaa,的lastIndexOf("/")是2 ...

  2. tomcat配置去掉项目名称

    在web项目中,把代码部署到服务器上访问时都不带项目名,可以配置tomcat 在tomcat安装目录下,找到conf/server.xml打开 <Host name="localhos ...

  3. xshell连不上虚拟机

    一般都是下边这种情况 查看 虚拟机的ip   ip a 看看是否有IP地址 如果没有的话,win+r 输入services.msc 把这三个服务设为正在运行状态 #虚拟机连不上网 前戏: 查看xshe ...

  4. wxPython制作跑monkey工具(python3)-带显示设备列表界面

    一. wxPython制作跑monkey工具(python3)-带显示设备列表界面  源代码 Run Monkey.py #!/usr/bin/env python import wx import ...

  5. Ubuntu 安装 uget

    PPA方式 sudo add-apt-repository ppa:plushuang-tw/uget-stable sudo apt update sudo apt install uget -y ...

  6. 转 Postman访问Webapi的Get/Post/Put/Delte请求

    Postman访问Webapi的Get/Post/Put/Delte请求 2018年07月26日 15:04:46 DoNotWorkOvertime 阅读数:348 标签: WebApiPostma ...

  7. UVa439——骑士的移动

    简单bfs #include <iostream> #include <cstring> #include <string> #include <map> ...

  8. linux文件查找find命令

    linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find  [option ...

  9. css常用样式总结:

    一.input和textarea修改placeholder的颜色: input::-webkit-input-placeholder { /* WebKit browsers */ color: #c ...

  10. HOMEWORK1

    回顾你过去将近3年的学习经历 当初你报考的时候是真正喜欢计算机这个专业吗? 当初报考的时候是选择英语和计算机专业,报英语那个学校没去上,就来学了计算机,对计算机专业的感觉介于喜欢和热爱之间,就是说还是 ...