JavaScript函数导入无法正常工作

时间:2019-04-11 06:37:10

标签: javascript

我指的是下面的代码。 https://github.com/jonasschmedtmann/complete-javascript-course/blob/master/9-forkify/final/src/js/views/searchView.js

我制作了一个函数,并尝试将该函数导入另一个js文件,但是它不起作用。如果我写“ elements.searchInput.value;”如果我执行“ console.log(elements.searchInput.value)”,它将无法正常工作。我在做与参考文件完全相同的操作,只是想知道为什么我的代码不起作用。

index.js

import "./../styles/style.scss";
import Search from './models/Search';
import * as searchView from './views/SearchView';
import { key, proxy } from './config';
import { elements } from './views/Base';

const state = {};

const controlSearch = async () => {
    const query = searchView.getInput(); // <- This doesn't work
    console.log(query) // Get nothing

    if(query) { // <- Can't get in to this part
        state.search = new Search(query);
        console.log("New Search", state);
    }
}

elements.searchForm.addEventListener('submit', e => {
    event.preventDefault();
    controlSearch();
});

searchView.js

import { elements } from './base';

export const getInput = () => {
    elements.searchInput.value; // This code won't show up in controlSearch function

export const clearInput = () => {
    elements.searchInput.value = '';
};
base.js

export const elements = {
    searchForm: document.querySelector('.search'),
    searchResult: document.querySelector('.movie_list'),
    searchInput: document.querySelector('.search__field')
}
Search.js

import { key } from '../config';

export default class Search {
    constructor(query) {
        this.query = query;
    }

    async getResults() {
        try {
            const res = await axios(`http://`);
            this.result = res.data.results;
        }

        catch (error) {
            alert(error);
        }
    }

}

2 个答案:

答案 0 :(得分:3)

字母功能必须返回一个值。

select Id,
  name,
  max(case when rn = 1 then StartDate end) StartDate,
  max(case when rn = 1 then EndDate end) EndDate,
  max(case when rn = 1 then Association end) Association,
  max(case when rn = 2 then StartDate end) StartDate1,
  max(case when rn = 2 then EndDate end) EndDate1,
  max(case when rn = 2 then Association end) Association1
from
(
  select id, name, StartDate, EndDate, Association,
    row_number() over(partition by Id order by name) rn
  from Business
) src
group by id, name;

或通过箭头功能可以使用:

const yourFunction = () => { return yourVariable };

答案 1 :(得分:2)

这是您的问题

export const getInput = () => {
    elements.searchInput.value; 

您什么都没退 更改为

export const getInput = () => elements.searchInput.value; 

export const getInput = () => {
    return elements.searchInput.value; 
}