Функция не найдена в пользовательском модуле - VueJS - PullRequest
0 голосов
/ 05 марта 2019

Я пишу две функции в модуле для использования в некоторых разделах гибридного мобильного приложения.Имя модуля "functs.js":

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;
    }
}

Когда я вызываю TRUNCATE_LETTERS и комментирую TRUNCATE_WORDS, все идет нормально, но при раскомментировании я получаю эту ошибку на CLI:

warning  in ./src/views/Offers.vue?vue&
type=script&lang=js&

"export 'TRUNCATE_LETTERS' was not found
 in '@/components/functs'

Я протестировал две функции в отдельном файле HTML и не получил никаких ошибок.

Есть что-то, чего я не видел?Мне нужно усечь слова, а не буквы.

Спасибо за любую помощь.

1 Ответ

0 голосов
/ 05 марта 2019

Вот правильный синтаксис:

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";
...