Laravel Mix编译前端资源

Laravel Mix编译前端资源
原文地址

 

目前项目是使用的vue+laravel来写的,其中laravel和vue分别放了一个目录,但是这样有个问题,那就是vue需要经常更新,不然运行项目会经常出现各种问题,这里就看了看laravel的文档,才知道还有Mix这个组件,进行编译前后端资源。下载完成后的目录和laravel是一样的,只是在根目录下面有一个node_models目录。

安装:

安装Node,首先要确保自己的电脑上面有安装Node。

使用:node -v    和   npm -v查看是否有版本号,有即表示有安装Node,否则需要安装Node。

cdn.jsdelivr.net/npm/vue,下载最新的Node。

接下来就需要安装Laravel Mix,首先安装Laravel项目:composer create-project --prefer-dist laravel/laravel blog  这里项目的名字叫blog  。

安装成功后,进入安装目录。

安装前端依赖:npm install(这里可以选择淘宝镜像)

使用例子:

修改 routes/web.php 文件为:

Route::get('/',function(){

return view('index');

});

新建一个Hello.vue文件

在 resources/assets/js/components 目录下新建 Hello.vue 文件


 
 
  1. <template>
  2. <div>
  3. <h1>Hello, Larvuent! </h1>
  4. <p class="hello">{{ msg }} </p>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. msg: 'This is a Laravel with Vue and Element Demo.'
  12. }
  13. }
  14. }
  15. </script>
  16. <style>
  17. .hello {
  18. font-size: 2em;
  19. color: green;
  20. }
  21. </style>

修改 app.js 文件

修改 resources/assets/js/app.js 文件


 
 
  1. require( './bootstrap');
  2. window.Vue = require( 'vue');
  3. // Vue.component('example', require('./components/Example.vue')); // 注释掉
  4. import Hello from './components/Hello.vue'; // 引入Hello 组件
  5. const app = new Vue({
  6. el: '#app',
  7. render: h => h(Hello)
  8. });

新建 Laravel 视图文件,和 Vue 交互

在 resources/views 目录下新建 index.blade.php 文件


 
 
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Larvuent </title>
  6. </head>
  7. <body>
  8. <div id="app"> </div>
  9. <script src="{{ mix('js/app.js') }}"> </script>
  10. </body>
  11. </html>

编译:运行  npm run dev

提示编译成功,并访问页面:输入http://192.168.1.112

显示如下表示成功:

 

现在已经完成vue+laravel了,还需要引入element.

npm i element-ui -S   即可引入element

修改 resources/assets/js/app.js 文件


 
 
  1. import Hello from './components/Hello.vue'; // 引入Hello 组件
  2. import ElementUI from 'element-ui';
  3. import 'element-ui/lib/theme-default/index.css';
  4. Vue.use(ElementUI);

修改Hello.vue,使用element组件。


 
 
  1. <template>
  2. <div>
  3. <h1>Hello, Larvuent! </h1>
  4. <el-button @click="visible = true">按钮 </el-button>
  5. <el-dialog v-model="visible">
  6. <p>欢迎使用 Element </p>
  7. </el-dialog>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. visible: false
  15. }
  16. }
  17. }
  18. </script>
  19. <style>
  20. .hello {
  21. font-size: 2em;
  22. color: green;
  23. }
  24. </style>

再次运行npm run dev  进行编译前端资源。然后访问页面如下:

这里修改了一个vue文件就要重新编译一次,很麻烦,所有可以运行  npm run watch 就可以不需要再次编译了,每次修改过后的代码自动就会编译。

这里还有一个很重要的问题,那就是很多人参照以上的步骤会报错,并不会出现正常的页面。

很多人都会出现这样的问题。

所有需要在index页面加一个header头,就可以了。

修 改  resources/views/index.blade.php  文件为

目前都只是最基本的使用,大型项目是需要使用vue路由的。所有这里需要安装路由。

运行  npm install vue-router --save-dev  安装路由。

配置:

在 resources/assets/js 目录下新建目录 router ,同时在 router 目录下新建 index.js 文件


 
 
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. Vue.use(VueRouter);
  4. export default new VueRouter({
  5. saveScrollPosition: true,
  6. routes: [
  7. {
  8. name: 'hello',
  9. path: '/hello',
  10. component: resolve => void( require([ '../components/Hello.vue'], resolve))
  11. }
  12. ]
  13. });

在 resources/assets/js 目录下新建 App.vue 文件


 
 
  1. <template>
  2. <div>
  3. <h1>Hello, {{ msg }}! </h1>
  4. <router-view> </router-view> <!--路由引入的组件将在这里被渲染-->
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. msg: 'Vue'
  12. }
  13. }
  14. }
  15. </script>
  16. <style>
  17. </style>

修改 resources/assets/js/app.js 文件为


 
 
  1. // import Hello from './components/Hello.vue';
  2. import App from './App.vue';
  3. import ElementUI from 'element-ui';
  4. import 'element-ui/lib/theme-default/index.css';
  5. Vue.use(ElementUI);
  6. import router from './router/index.js';
  7. const app = new Vue({
  8. el: '#app',
  9. router,
  10. render: h => h(App)
  11. });

重新编译。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值