拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb

效果

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>js引入vue.js实现vue</title>
<link rel="stylesheet" href="css/1.css">
<script src="js/jquery.js"></script>
<script>
function htmlFont(standardWid) {
var explorer = navigator.userAgent,
html = document
.getElementsByTagName('html')[0];
standardWid = (typeof standardWid == 'number') ? standardWid : 375;
if (explorer.indexOf("Opera") >= 0) {
winwid = screen.width;
} else {
winwid = html.offsetWidth;
}
if (winwid > 750) {
fosi = 200;
} else {
winwid = winwid > standardWid * 2 ? standardWid * 2 : winwid;
fosi = winwid / standardWid * 100;
}
html.style.fontSize = fosi + 'px';
}
htmlFont();
</script>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script>
<script src="js/1.js"></script>
</head>
<body>
<div id="app" v-cloak>
<transition :name="transitionName">
<keep-alive>
<router-view class="child-view" v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
</transition> <transition :name="transitionName">
<router-view class="child-view" v-if="!$route.meta.keepAlive"></router-view>
</transition>
</div>
</body>
</html>

js

'use strict';
$(document).ready(function() {
// 0. 如果使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter)
Vue.use(VueRouter); // 1. 定义(路由)组件。
// 可以从其他文件 import 进来 Vue.prototype.$fun1 = function() { //全局函数1
console.log('fun1');
}; var Vue1 = Vue.extend({
data() {
return {}
},
computed: {},
methods: {
toVue2: function() {
this.$router.push({
name: 'Vue2'
})
}
},
template: "<div class='d-vue1'><p class='redBc' @click='toVue2'>组件1</p>" +
"<p class='orangeBc' @click='$fun1'>点击</p></div>"
}) Vue.component(Vue1) const Vue2 = Vue.extend({
template: "<div class='d-vue2'><p class='greenBc'>组件2</p>" +
"<p class='orangeBc' @click='$fun1'>点击</p></div>"
}) Vue.component(Vue2) // 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
const routes = [{
path: '/',
component: Vue1
},
{
path: '/vue2',
component: Vue2
}
] // 3. 创建 router 实例,然后传 `routes` 配置
const router = new VueRouter({
routes: [{
path: '/',
name: 'Vue1',
meta: {
index: 0,
keepAlive: true,
title: '组件1'
},
component: Vue1
},
{
path: '/vue2',
name: 'Vue2',
meta: {
index: 1,
keepAlive: false,
title: '组件2'
},
component: Vue2
}
]
}) router.beforeEach((to, from, next) => { const toDepth = to.meta.index;
const fromDepth = from.meta.index; if (to.meta.title) {
document.title = to.meta.title;
} if (toDepth < fromDepth) { //返回
from.meta.keepAlive = false;
to.meta.keepAlive = true; //相当于缓存
}
next()
}) // 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
el: '#app',
data: {
transitionName: ''
},
watch: {
$route(to, from) {
if (to.meta.index > from.meta.index) {
this.transitionName = 'slide-left';
} else {
this.transitionName = 'slide-right';
}
}
},
router
}).$mount('#app')
})

css

.child-view {
position: absolute;
width: 100%;
transition: all .5s cubic-bezier(.55, 0, .1, 1);
} .slide-left-enter,
.slide-right-leave-active {
opacity: 0;
-webkit-transform: translate(50px, 0);
transform: translate(50px, 0);
} .slide-left-leave-active,
.slide-right-enter {
opacity: 0;
-webkit-transform: translate(-50px, 0);
transform: translate(-50px, 0);
} body,
div,
p {
margin: 0;
padding: 0;
color: #fff;
text-align: center;
font-weight: 500 !important;
} [v-cloak] {
display: none
} p {
margin-top: .5rem;
} .redBc {
background-color: red;
} .orangeBc {
background-color: orange;
} .greenBc {
background-color: green;
}

若需请求接口,html增加引入:

<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>

使用:

function getData(){
var param1 = {
    param: {
    id:'1',
sex:'male'
}
}
axios.get( "url", {params: param1}).then(function (res) { //url为请求接口, 当请求需要参数为JSON数据时
// axios.get( "url?param2="+"XXX").then(function (res) { //当请求参数无需用JSON时,param2为请求需要参数
if (res.status == 200 && res.data.result == 0) {
var _data = res.data.message;
}else{}
})
.catch(function (error) {
console.log(error);
});
}

js 引入Vue.js实现vue效果的更多相关文章

  1. 如何在其他js 引入main.js 中 vue 的实例?

    1.原因解析 经测试发现,代码先执行了 index.js >>  main.js >> Home.vue scr/api/index.js src/main.js src/co ...

  2. Vue.js 引入外部js方法

    1.外部文件config.js 第一种写法 //常量的定义 const config = { baseurl:'http://172.16.114.5:8088/MGT2' } //函数的定义 fun ...

  3. js引入,js变量和运算符等

    页面级的js不管写在页面的哪里都可以 企业项目开发要求:结构(html),样式(css),行为(js)相分离 不要既写外部js,又写内部js:如果两个都写,则外部js生效 声明多个变量时,每个变量之间 ...

  4. 第10章-Vue.js 项目实战

    一.本节内容 掌握项目环境中路由的配置方法 ***** 熟练掌握编写单文件组件的编写 *** 能够使用swiper.js进行轮播图组件的封装 能够使用axios进行数据请求 二.webpack项目的目 ...

  5. 【实战】Vue全家桶(vue + axios + vue-router + vuex)搭建移动端H5项目

    使用Vue全家桶开发移动端页面. 本博文默认已安装node.js. github链接 一.准备工作 安装vue npm install vue 安装脚手架vue-cli npm install -g ...

  6. vue引入swiper vue使用swiper vue脚手架使用swiper /引入js文件/引入css文件

    vue引入swiper  vue使用swiper  vue脚手架使用swiper /引入js文件/引入css文件 ------------------------------------------- ...

  7. Vue.js实现tab切换效果

    利用Vue实现简易tab切换效果 1.1 在我们平时浏览网站的时候,经常看到的特效有图片轮播.导航子菜单的隐藏.tab标签的切换等等.这段时间学习了vue后,开始要写出一些简单的特效. 1.2 实现思 ...

  8. js 仿微信投诉—引入vue.js,拆分组件为单个js

    效果 页面目录 index.html <!DOCTYPE html > <html> <head> <meta charset="UTF-8&quo ...

  9. vue.js引入

    开始学习vue.js,引入vue.vue.js一定要在head里面引入,实际开发中我们可能在body中引入,但是可能存在抖屏现象. 为了避免出现抖屏现象,我们引入vue.js或者jquery.js 最 ...

随机推荐

  1. Face-Resources

    Face-Resources Following is a growing list of some of the materials I found on the web for research ...

  2. gradle 随记

    gradle项目下添加jar包 compile fileTree(dir: './src/main/resources/lib', include: '*.jar') 将jar包放到这个目录下./sr ...

  3. Java 基础 - 如何重写equals()

    ref:https://www.cnblogs.com/TinyWalker/p/4834685.html -------------------- 编写equals方法的建议: 显示参数命名为oth ...

  4. 【JZOJ6274】梦境

    description analysis 其实可以贪心 先把区间按左端点排序,转折点也排序 扫一次转折点,把所有左端点在当前点左边的区间丢进优先队列里 按照贪心策略,对于某个转折点,一定选择右端点离它 ...

  5. 校园商铺-4店铺注册功能模块-4Dto之ShopExecution的实现

    1. DTO:添加店铺的返回类型 问题:为什么不直接用实体类Shop呢? 原因:在操作Shop的时候,必然会有一个状态.添加店铺,添加成功,还是添加失败? 如果添加失败,失败是一个什么状态,这些都是要 ...

  6. Heartbeat基本介绍----HA / vmware HA FT

    Heartbeat是High-Availability Linux Project (Linux下的高可用性项目)的产物,是一套提供防止业务主机因不可避免的意外性或计划性宕机问题的高可用性软件.Hea ...

  7. JS switch 分支语句

    描述:根据一个变量的不同取值,来执行不同的代码. 语法结构: switch(变量) { case 值1: 代码1; break; case 值2: 代码2; break; case 值3: 代码3; ...

  8. 17.获取代理ip

    import redis import telnetlib import urllib.request from bs4 import BeautifulSoup r = redis.Redis(ho ...

  9. 第三周课堂笔记4thand5th

    循环打印 #计算字典中的键值对的个数 print(len(a)) #获取字典中键的列表 print(a.keys()) #获取字典中值的列表 print(a.values()) #获取字典中键值对的个 ...

  10. Spring NamedParameterJdbcTemplate详解(10)

    NamedParameterJdbcTemplate和JdbcTemplate功能基本差不多.使用方法也类型.下面具体看下代码. db.properties 1 jdbc.user=root 2 jd ...