为了账号安全,请及时绑定邮箱和手机立即绑定

在哪里添加导入命令

在哪里添加导入命令

白衣非少年 2021-09-30 10:11:50
我正在尝试在我的 Laravel 项目中实现一个毒性 js 我正在使用这个库 https://www.npmjs.com/package/@tensorflow-models/toxicity但是当我运行它时,我会在控制台中收到此错误Uncaught ReferenceError: toxicity is not defined我的代码看起来像这个 atm看法<div id="result"></div>myjs.js//import * as toxicity from '../../node_modules/@tensorflow-models/toxicity'; //doesnt work//import * as toxicity from '@tensorflow-models/toxicity'; //doesnt work$(document).ready(function() {    const threshold = 0.9;// Load the model. Users optionally pass in a threshold and an array of// labels to include.    toxicity.load(threshold).then(model => {        const sentences = ['you suck'];        model.classify(sentences).then(predictions => {            // `predictions` is an array of objects, one for each prediction head,            // that contains the raw probabilities for each input along with the            // final prediction in `match` (either `true` or `false`).            // If neither prediction exceeds the threshold, `match` is `null`.            console.log(predictions);            /*            prints:            {              "label": "identity_attack",              "results": [{                "probabilities": [0.9659664034843445, 0.03403361141681671],                "match": false              }]            },            {              "label": "insult",              "results": [{                "probabilities": [0.08124706149101257, 0.9187529683113098],                "match": true              }]            },            ...             */        });    });});我认为问题是我不知道我应该在 Laravel 中的何处包含这一行import * as toxicity from '@tensorflow-models/toxicity';如果我将它包含在 js 文件中,它会给出一个错误:Uncaught SyntaxError: Unexpected token *
查看完整描述

3 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

好的,所以我设法通过进入 bootstrap.js 文件并添加这个来修复这个错误

window.toxicity = require('@tensorflow-models/toxicity');

然后运行

npm run dev


查看完整回答
反对 回复 2021-09-30
?
郎朗坤

TA贡献1921条经验 获得超9个赞

尝试将其添加到资源/js 文件夹中的 app.js 文件然后在您的终端中运行命令

npm run dev

然后再次尝试使用它。希望这可以帮助



查看完整回答
反对 回复 2021-09-30
  • 3 回答
  • 0 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信