canihelpyou

510 经验值

我想发送一个ajax请求检测用户是否登录,如果没有登录就跳转到登录页面,怎么写才可以?

 axios.get('/check-login')
       .then(response => {
              this.dialogFormVisible = true; //如果登录就执行这个语句
          })
         .catch(error => {
              location.href = "/login";  //如果登录就执行跳转到登录页
});

控制器CheckController.php

    public function checkLogin()
    {
        $status=Auth::check();
        return $status;
    }
<div class="container" id="app">
    <div class="card">
        <h3 class="card-header">Todos 数量:  todoCount  </h3>
        <div class="card-block">
            <todo-items :todos="todos"></todo-items>
        </div>
        <div class="card-block">
            <todo-form :todos="todos"></todo-form>
        </div>
    </div>

</div>

<script type="text/x-template" id="todo-items-template">
    <ul class="list-group">
        <li class="list-group-item" v-bind:class="{ 'completed': todo.completed }" v-for="(todo,index) in todos">
              todo.title  
            <button type="button" class="btn btn-sm" v-bind:class="[todo.completed ? 'btn-success': 'btn-danger']" v-on:click="toggleCompletion(todo)">  todo.completed ? 'completed': 'undo'  </button>
            <button type="button" class="btn btn-warning btn-sm"  v-on:click="deleteTodo(index)">删除</button>
        </li>
    </ul>
</script>

<script type="text/x-template" id="todo-add-form-template">
    <form v-on:submit.prevent="addTodo(newTodo)">
        <div class="form-group row">
            <label for="input1" class="col-sm-2 col-form-label">输入内容</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="input1" v-model="newTodo.title">
            </div>
        </div>
        <div class="form-group row">
            <div class="offset-sm-2 col-sm-10">
                <button type="submit" class="btn btn-primary">提交</button>
            </div>
        </div>
    </form>
</script>

<script>
    Vue.component('todo-items',{
        template:"#todo-items-template",
        props:['todos'],
        methods:{
            deleteTodo(index){
                this.todos.splice(index,1);
            },
            toggleCompletion(todo){
                todo.completed = !todo.completed
            }
        }
    });

    Vue.component('todo-form',{
        template:"#todo-add-form-template",
        props:['todos'],
        data(){
            return{
                newTodo: {id:null,title:'',completed:false}
            }
        },
        methods:{
            addTodo(newTodo){
                this.todos.push(newTodo);
                this.newTodo={id:null,title:''};
            }
        }
    });

    new Vue({
        el: "#app",
        data:{
            todos:[
                {id:1,title:'learn vue.js 2.0',completed:false}
            ]
        },
        computed:{
            todoCount(){
                return this.todos.length;
            }
        }
    });
</script>

问题:
props:['todos']中的todos对应的下面两个标签中的:todos="todos",是对应等号前面的todos还是后面的?

<todo-items :todos="todos"></todo-items>
<todo-form :todos="todos"></todo-form>

vue组件的name属性是拿来做什么的?

export default {
    name: 'address',
 //...
}

jQuery阻止表单默认提交行为后,要继续进行提交行为,需要怎么做?

具体是这样的:

一个删除按钮,在提交之前用sweetalert2提示一下,当按下提示框中的“确认”按钮后,继续提交表单。
html代码:

<form action="/articles/  $article->id  " method="POST">
      method_field('DELETE')  
      csrf_field()  
    <input class="btn btn-danger btn-sm need-alert" type="submit" value="删除">
</form>

js代码:

    <script>
        $('.need-alert').on('click', function (event) {
            //阻止默认提交事件
            event.preventDefault();
            
            //sweetalert2的提示框代码:
            swal({
                title: '确定要删除吗?',
                text: '删除后不能恢复!',
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: '确认删除'
            }).then(function () {
                
                
            }).catch(swal.noop);
        })
    </script>

preventDefault()阻止默认提交事件后,sweetalert2提示框可以正常弹出。

问题:

当点击sweetalert2提示框的“确认删除”后,要继续提交这个表单,要怎么写?

laravel用npm安装一个包之后如何引入?

比如,我要使用sweetalert2,先安装:

npm install --save sweetalert2

在laravel的\resources\assets\js\bootstrap.js的文件中,默认内容是这样的:

window._ = require('lodash');

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}


window.axios = require('axios');

window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

上面内容看起来好像是引入了lodash,jquery,bootstrap,axios,但它们的写法不同,分别是:

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');

问题:
1、现在我要引入sweetalert2,应该怎么写?
2、能否解释一下上面4种写法分别是什么意思?

laravel分组中的路由要访问分组外面的控制器怎么办?

比如下面的路由:

Route::get('/settings', 'SettingsController@index');
Route::group(['prefix' => 'user', 'namespace' => 'User', 'middleware' => ['auth', 'role:user']], function () {
    Route::resource('dashboard', 'DashboardController');
    Route::get('/settings', 'SettingsController@index'); //这里的本意是要访问外面的SettingsController
});

分组中的SettingsController那条路由本意是要访问外面的SettingsController,要怎么写才可以?

使用vuejs时,变量在a标签中怎么解析?
比如下面这个:

<a href="/articles/@  item.id  /edit"></a>

按F12在浏览器调试窗口中看到的是这样的,解析不出来:

<a href="/articles/  item.id  /edit"></a>

要怎么写才可以?

laravel5.4自带的auth生成的路由后面有个name()表示什么意思?
如下所示:

Route::get('/home', 'HomeController@index')->name('home');

name('home')表示什么意思?