Vue.js 在自定义模块-VueJS中找不到函数
我在一个模块中编写了两个函数,用于混合移动应用程序的某些部分。模块名称为functs.js:Vue.js 在自定义模块-VueJS中找不到函数,vue.js,module,vuejs2,vue-router,Vue.js,Module,Vuejs2,Vue Router,我在一个模块中编写了两个函数,用于混合移动应用程序的某些部分。模块名称为functs.js: module.exports = { TRUNCATE_LETTERS (txt,max) { const limit = max || 15; const text = (txt && txt.trim()) ? txt.trim() : ''; const dots = '...'; let resp = '';
module.exports = {
TRUNCATE_LETTERS (txt,max) {
const limit = max || 15;
const text = (txt && txt.trim()) ? txt.trim() : '';
const dots = '...';
let resp = '';
if ( txt.length > limit )
{
resp = txt.substring(0, limit) + ' ' + dots;
} else {
resp = text + ' ' + dots;
}
return resp;
},
TRUNCATE_WORDS (txt,max) {
const limit = max || 10;
const text = (txt && txt.trim()) ? txt.trim() : '';
const dots = '...';
let resp = '';
const arr = text ? text.split(' ') : [];
let newArr = [];
if ( arr.length > limit )
{
for ( let i = 0; i < limit; i++ )
{
newArr.push( arr[i] );
}
resp = newArr.join(' ') + ' ' + dots;
} else {
resp = text + ' ' + dots;
}
return resp;
}
}
我在一个单独的HTML文件中测试了这两个函数,没有收到任何错误
有什么我没看见的吗?我需要截断单词,而不是字母
谢谢您的帮助。以下是正确的语法:
module.exports = {
TRUNCATE_LETTERS: function(txt,max) { ... },
TRUNCATE_WORDS: function(txt,max) { ... }
}
Use :
const { TRUNCATE_LETTERS, TRUNCATE_WORDS } = require("/path/mymodule");
or
const TRUNCATE_LETTERS = require("/path/mymodule").TRUNCATE_LETTERS ;
在VueJs中使用导出默认值/导入:
const truncate = {
TRUNCATE_LETTERS: function(txt,max) { ... },
TRUNCATE_WORDS: function(txt,max) { ... }
}
export default truncate;
Use:
import truncate from "/path/mymodule";
truncate.TRUNCATE_LETTERS(...);
or
import { TRUNCATE_LETTERS, TRUNCATE_WORDS } from "/path/mymodule";
经过多次尝试和错误后,即使使用您的示例,我最终还是倾向于导出每个函数,并从“@/components/functs”使用import{TRUNCATE_WORDS,…}。现在一切都好了。谢谢你的帮助-好现在我收到了很多警告,包括我后来添加的一个新函数:警告出现在./src/components/nav.vue中?vue&type=script&lang=js&export在“@/components/functs”中找不到“RANDOM_ID”对不起,我最好继续导出每个函数,因为这样对我有用-
const truncate = {
TRUNCATE_LETTERS: function(txt,max) { ... },
TRUNCATE_WORDS: function(txt,max) { ... }
}
export default truncate;
Use:
import truncate from "/path/mymodule";
truncate.TRUNCATE_LETTERS(...);
or
import { TRUNCATE_LETTERS, TRUNCATE_WORDS } from "/path/mymodule";