可能是人老了接受能力变弱了~~总之Vue的调用真是绕晕我,不过也相信后面慢慢会能掌握使用,这次Mark一下这两天新建项目的梳理。
一、修改端口号:找到config文件夹下面的index.js文件,修改里面的参数即可
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8099, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
二、xx.html 和xx.vue
做前端的都对html很熟悉了,Vue.js即提供了html的也提供自己的vue写法,下面对比一下:
1、xx.html:这里用默认的开始项index.html文件作为示范
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>test-pro2</title>
<script src="https://unpkg.com/vue"></script>
<style>
p{
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<!--真实vue代码不写这里,写在App.vue即可-->
</div>
<div id = "test">
<p>测试:{{ message }}</p>
</div>
<!-- built files will be auto injected -->
</body>
<script>
new Vue({
el: '#test',
data: {
message: 'Hello Vue.js!'
}
})
</script>
</html>
2、xx.vue:这里用App.vue作为示范
<template>
<div id="app">
<!--<router-view></router-view>--><!--为了展现区别,把原来调用的组件隐藏,后面会讲如何调用组件-->
<h1>{{hw}}</h1>
</div>
</template>
<script>
export default {
name: 'app',
data(){
return{
hw:'HelloWorld'
}
},
components:{
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
1和2的效果图如下:
三、既然有心学习vue,我们就要学会怎么引入使用组件这个概念,不能依赖于自己熟悉的html页面
我们先把index.html修改一下,删除刚刚上面写的html写法,剩下vue需要的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>test-pro2</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<!--真实代码不写这里,写在App.vue即可-->
</div>
</body>
</html>
1、在components下我们新建两个组件
2、TestComponent1.vue
<template>
<div class="testcone">
<h1>{{ msg }}</h1>
<h2>{{p1}}</h2>
</div>
</template>
<script>
export default {
name: 'testcone',
data () {
return {
msg: 'This is TestComponent1',
p1: '111111'
}
}
}
</script>
<style>
</style>
3、TestComponent2.vue:
<template>
<div class="testctwo">
<h1>{{ msg }}</h1>
<h2>{{p1}}</h2>
</div>
</template>
<script>
export default {
name: 'testctwo',
data () {
return {
msg: 'This is TestComponent2',
p1: '222222'
}
}
}
</script>
<style>
</style>
4、在App.vue:引用上面写好的组件写好组件的引用方法名
<template>
<div id="app">
<!--<router-view></router-view>-->
<TestCOne></TestCOne>
<h1>{{hw}}</h1>
<TestCTwo></TestCTwo>
</div>
</template>
<script>
import TestCOne from './components/TestComponent1.vue'
import TestCTwo from './components/TestComponent2.vue'
export default {
name: 'App',
data () {
return {
hw: 'HelloWorld'
}
},
components: {
TestCOne, TestCTwo
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
效果图如下:
如果这些文件关系还不是很清楚的话,可以参考这篇文章:
https://blog.csdn.net/zbl744949461/article/details/80476888#commentBox