是否可以在vue 2中使用没有组件的vue-router?

时间:2017-12-06 15:53:58

标签: vue.js vuejs2 vue-component vue-router

我有一个非常简单的页面,只有几行代码,我想使用没有vue组件的vue-router。

我想在根目录后获得class GenericEntity<T extends Serializeable>

string "my-search-query"

并在我的主Vue()对象中使用它。这可能吗?

谢谢!

3 个答案:

答案 0 :(得分:4)

可以在没有conditions = [] conditions.append(yourVar < 9) conditions.append(yourVar > 20) conditions.append(yourVar <= 3) if all(conditions): print('All conditions are met') else: print('At least one condition results in false) 的情况下使用vue-router,并且仍然会收到有关主Vue()应用对象中路径更改的通知:

您可以从vue-component获取"my-search-query"字符串,如下所示:

www.example.com/#/my-search-query

感谢@samayo指出我正确的方向!

答案 1 :(得分:1)

正如tom_h所说,如果您只想获取路径名,可以使用window.location.pathname。如果您希望在此路径名更改时收到通知,请先将其初始化为您的data(){}对象并将其视为:

data () {
  return {
    path: window.location.pathname
  }
},

watch: {
  path (newpath, oldpath) {
    alert('location path just changed from ' + newpath + ' to ' + oldpath )
  }
}

答案 2 :(得分:1)

由于路由器是视图模型实例的一部分,因此您可以创建一个计算函数,该函数返回路由器的路径值。例如,您可以执行以下操作:

a_list = []

for x in range(5):
    a_list.append(dict())
    for y in range(5):
        if (x != 0 and y != 0) and (x * x != x * y):
            a_list[-1][y] = x * y

这样,路由器将为您管理历史记录,但是您不必将代码分成多个部分。例如,我有一个很小的页面,只是想将所有内容放入同一个viewModel中而不将其分解为组件。

相关问题