以下实例代码地址: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. MSP430中断的一个细节问题

    关于中断标志: 从SPI发送一字节数据: void SPI_Set_SD_Byte(unsigned char txData) { UCB0TXBUF = txData; // 写入发送缓冲区 whi ...

  2. nginx 配置本地https(免费证书)

    Linux系统下生成证书 生成秘钥key,运行: $ openssl genrsa -des3 -out server.key 20481会有两次要求输入密码,输入同一个即可 输入密码 然后你就获得了 ...

  3. 转换流 InputStreamReader

    通常接触到字节流和字符流,但是有一个流是这两个流的桥梁,inputStreamReader 字符流的结构如下 可以看到inputStreamReader是继承Reader ,它的子类是FileRead ...

  4. jasperreports+IReport 5.56,集成到Spring MVC4.0案例

    首先,先说一下需求,项目需要打印一些报表,也没多想,直接就在jsp页面设置了样式,前台直接调用window.print()写了打印功能,但是例会的时候,领导提出需要一些比较麻烦的打印,自己写肯定费时间 ...

  5. 使用Axure做验证码之校验验证码(二)

    本次作业,输入验证码,并校验验证码是否正确.上篇文章,介绍了如何获取验证码,本次作业在上次作业的基础上,做进一步的深究. 1.在上次作业中,增加新的元件: 文本框,命名:输入验证码: 增加热区,命名为 ...

  6. AC, FVOCI, FVPL

    IFRS9 会计新准则中规定了资产划分依据为合同现金流特征及业务模式. 关键词:资产负债表,利润表,损益,利率风险 AC账户: 为收取合同约定的现金流.持有到期业务. 资产在持有期内不变,使用摊余成本 ...

  7. Failed to load ApplicationContext ,Error creating bean with name 'adminUserService': Injection of autowired dependencies failed;

    Druid配置的时候出现这个问题: "C:\Program Files\Java\jdk1.8.0_191\bin\java" -ea -Didea.test.cyclic.buf ...

  8. Lesson Learned

    最近,中兴ZTE违反美国商务部禁令,向伊朗出售敏感技术,被美国下达长达7年的禁止令,教训十分深刻.以诚待人,信守承诺,才能在商业社会站稳脚跟. 还是说说最近自己上的一课吧.上了港台服以后,奇奇怪怪的问 ...

  9. 2018.9.12 B树总结

    1. B-Tree B-树是一种平衡的多路查找树,它在文件系统中很有用. 1.1 B-Tree 特性 关键字集合分布在整颗树中: 任何一个关键字出现且只出现在一个结点中: 搜索有可能在非叶子结点结束: ...

  10. SOCKET.IO 的用法 系统API,

    原文:http://www.cnblogs.com/xiezhengcai/p/3956401.html 1. 服务端 io.on('connection',function(socket)); 监听 ...