微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将参数传递给刀片或{{}}?

如何解决如何将参数传递给刀片或{{}}?

对不起,我的英语不好。我来自台湾。

代码

AT+MQTTPUB=1,1,"topic_name","{\"menu\":{\"id\":\"1\",\"value\":\"2\"}}"

我想让它成为

        var channel = pusher.subscribe('question-channel');
        channel.bind('question-event',function (data) {

                var n = new Notification(data.title,{
                    icon: 'img/icon.png',body: data.content,image:'img/1.jpg'
                });

                n.onclick = function(e) { 
                    e.preventDefault(); // prevent the browser from focusing the Notification's tab
                    window.open('http://127.0.0.1:8000/'); 
                }

        });

但是, 我无法将参数(data.area)传递到 {{ }}

我必须这样做,以确认发布的是当前登录用户的专业知识之一。

解决方法

您有多种选择:

  1. 将您的数据传递到视图中并从 PHP 传递到 JS:

在何处调用视图:

use \App\Models\UserAreas;

// ...

    $userAreas = UserAreas::where('user_id','=',Auth::id())->get();
    return view('path/to/your/view',[ 'userAreas' => $userAreas ]);

在你的视野中:

        var userAreas = @json($userAreas);
        var channel = pusher.subscribe('question-channel');
        channel.bind('question-event',function (data) {

            if(userAreas.filter((area) => area.area_id === data.area).length)
                var n = new Notification(data.title,{
                    icon: 'img/icon.png',body: data.content,image:'img/1.jpg'
                });

                n.onclick = function(e) {
                    e.preventDefault(); // prevent the browser from focusing the Notification's tab
                    window.open('http://127.0.0.1:8000/');
                }

        });

  1. 使用 API 端点

api.php

use \App\Models\UserAreas;

// ...

Route::get('user-areas/{{userArea}}',function(int $userArea) {
    return (bool) UserAreas::where('user_id',Auth::id())->where('area_id',$userArea)->count();
});

your-view.blade.php

        var channel = pusher.subscribe('question-channel');
        channel.bind('question-event',function (data) {

            fetch('api/user-areas/' + data.area).then(function(exists) {
                if(exists) {
                    var n = new Notification(data.title,{
                        icon: 'img/icon.png',image:'img/1.jpg'
                    });

                    n.onclick = function(e) {
                        e.preventDefault(); // prevent the browser from focusing the Notification's tab
                        window.open('http://127.0.0.1:8000/');
                    }
                }
            }

        });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。