Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

thinkphp 配合 element 框架使用问题。

在非前后端分离的项目中使用element 想要使用this.$notify这种通知组件,代码如下

<script>
    axios.interceptors.response.use(response => {
        return response;
    }, err => {
        console.log(err.response.data.msg);
        this.$notify({
            title: err.response.data.msg,
            message: "",
            duration: 3000
        });
        Promise.reject(err);
    })
</script>

上面的代码报错。$notify not define .

教程提供的方式貌似是要用import 的方式引入,但是我这边不分离的框架貌似不支持import 的方式,同时也没有在Vue的实例里面,请问下要怎么支持这个通知组件的使用?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

其问题出在this 指向,按照你现在这种写的方式就不能这么用
首先确保引入 element-ui 库

<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

然后执行

<script> 
    axios.interceptors.response.use(response => {
        return response;
    }, err => {
        console.log(err.response.data.msg);
        window.ELEMENT.Notification({
            title: err.response.data.msg,
            message: "",
            duration: 3000
        })
        Promise.reject(err);
    }) 
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...